如何单击具有更多边界的 svg 线标记?

我每天都感谢开发者的回复。如何单击具有更多边界的 svg 线标记?

这是我的线路标签。

https://img4.mukewang.com/651d13770001acde02120216.jpg

这是我想要点击ling标签的边界

https://img1.mukewang.com/651d13870001828102120219.jpg

 <body>

    <svg height="210" width="500">

      <defs>

        <filter id="f1" x="0" y="0" width="200%" height="200%">

          <feOffset result="offOut" in="SourceGraphic" dx="2" dy="2" />

          <feBlend in="SourceGraphic" in2="offOut" mode="normal" />

        </filter>

      </defs>

      <line

        x1="0"

        y1="0"

        x2="200"

        y2="200"

        style="stroke:rgb(255,0,0);stroke-width:5;box-shadow: 10px 5px 5px red;"

      ></line>

    </svg>

  </body>

  <script>

    document

      .getElementsByTagName("line")[0]

      .addEventListener("click", function(e) {

        console.log("lineClick", e);

      });

  </script>

如何设置更多的点击事件边界?


慕莱坞森
浏览 87回答 1
1回答

撒科打诨

在第一条线的顶部添加第二条更宽的不可见线,捕获其点击并将其分派到下面的可见线。&nbsp; <body>&nbsp; &nbsp; <svg height="210" width="500">&nbsp; &nbsp; &nbsp; <defs>&nbsp; &nbsp; &nbsp; &nbsp; <filter id="f1" x="0" y="0" width="200%" height="200%">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <feOffset result="offOut" in="SourceGraphic" dx="2" dy="2" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <feBlend in="SourceGraphic" in2="offOut" mode="normal" />&nbsp; &nbsp; &nbsp; &nbsp; </filter>&nbsp; &nbsp; &nbsp; </defs>&nbsp; &nbsp; &nbsp; <line&nbsp; &nbsp; &nbsp; &nbsp; x1="0"&nbsp; &nbsp; &nbsp; &nbsp; y1="0"&nbsp; &nbsp; &nbsp; &nbsp; x2="200"&nbsp; &nbsp; &nbsp; &nbsp; y2="200"&nbsp; &nbsp; &nbsp; &nbsp; style="stroke:rgb(255,0,0);stroke-width:5;box-shadow: 10px 5px 5px red;"&nbsp; &nbsp; &nbsp; ></line>&nbsp; &nbsp; &nbsp; <line onclick="document&nbsp; &nbsp; &nbsp; .getElementsByTagName('line')[0].dispatchEvent(new Event('click'));"&nbsp; &nbsp; &nbsp; &nbsp; x1="0"&nbsp; &nbsp; &nbsp; &nbsp; y1="0"&nbsp; &nbsp; &nbsp; &nbsp; x2="200"&nbsp; &nbsp; &nbsp; &nbsp; y2="200"&nbsp; &nbsp; &nbsp; &nbsp; style="stroke:none;stroke-width:40;pointer-events:all;cursor:pointer"&nbsp; &nbsp; &nbsp; ></line>&nbsp; &nbsp; &nbsp; </svg>&nbsp; </body>&nbsp; <script>&nbsp; &nbsp; document&nbsp; &nbsp; &nbsp; .getElementsByTagName("line")[0]&nbsp; &nbsp; &nbsp; .addEventListener("click", function(e) {&nbsp; &nbsp; &nbsp; &nbsp; console.log("lineClick", e);&nbsp; &nbsp; &nbsp; });&nbsp; </script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5