JavaScript和SVG:如何增加onClick事件的可点击区域? [英] JavaScript and SVG: how do you increase the clickable area for an onClick event?

查看:148
本文介绍了JavaScript和SVG:如何增加onClick事件的可点击区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本在屏幕上以3的笔划宽度绘制线条.线条的大小(在视觉上是理想的),但是单击起来并不容易.

My script draws lines on the screen at a stroke-width of 3. The size of lines are ideal (visually) but they aren't very easy to click.

作为一个粗略的例子:

<html>
<head>
<script>
function selectStrand(evt) {
    current_id = evt.target.getAttributeNS(null, "id");

    document.getElementById('main').innerHTML = current_id;
}
</script>
</head>

<body>
Selected line: <span id="main"></span>

<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100%" preserveAspectRatio="xMinYMin meet" viewBox="0 0 1000 1000">
    <g id="buffer" transform="translate(10,0)">
      <line id="blue-blue" x1="50" y1="50" x2="500" y2="50" style="stroke:blue; stroke-width: 4;" stroke-linecap="round" onclick="selectStrand(evt)"/>
      <line id="blue-orange" x1="50" y1="70" x2="500" y2="70" style="stroke:orange; stroke-width: 4;" stroke-linecap="round" onclick="selectStrand(evt)"/>
      <line id="blue-green" x1="50" y1="90" x2="500" y2="90" style="stroke:green; stroke-width: 4;" stroke-linecap="round" onclick="selectStrand(evt)"/>
      <line id="blue-brown" x1="50" y1="110" x2="500" y2="110" style="stroke:brown; stroke-width: 4;" stroke-linecap="round" onclick="selectStrand(evt)"/>
      <line id="blue-grey" x1="50" y1="130" x2="500" y2="130" style="stroke:grey; stroke-width: 4;" stroke-linecap="round" onclick="selectStrand(evt)"/>
      <line id="blue-khaki" x1="50" y1="150" x2="500" y2="150" style="stroke:khaki; stroke-width: 4;" stroke-linecap="round" onclick="selectStrand(evt)"/>
      <line id="blue-red" x1="50" y1="170" x2="500" y2="170" style="stroke:red; stroke-width: 4;" stroke-linecap="round" onclick="selectStrand(evt)"/>
      <line id="blue-black" x1="50" y1="190" x2="500" y2="190" style="stroke:black; stroke-width: 4;" stroke-linecap="round" onclick="selectStrand(evt)"/>
      <line id="blue-yellow" x1="50" y1="210" x2="500" y2="210" style="stroke:yellow; stroke-width: 4;" stroke-linecap="round" onclick="selectStrand(evt)"/>
      <line id="blue-purple" x1="50" y1="230" x2="500" y2="230" style="stroke:purple; stroke-width: 4;" stroke-linecap="round" onclick="selectStrand(evt)"/>
      <line id="blue-pink" x1="50" y1="250" x2="500" y2="250" style="stroke:pink; stroke-width: 4;" stroke-linecap="round" onclick="selectStrand(evt)"/>
      <line id="blue-cyan" x1="50" y1="270" x2="500" y2="270" style="stroke:cyan; stroke-width: 4;" stroke-linecap="round" onclick="selectStrand(evt)"/>
    </g>
  </g>
</svg>

</body>
</html>

是否有一种简单的方法来增加每条线周围的面积,以使其更易于单击?

Is there a simple way to increase the area around each of the lines to make them easier to click?

推荐答案

对于每条线,尝试在其顶部绘制一条具有较大笔触宽度的透明线,并在其上设置onclick.

For each line, try drawing a transparent line on top of it with a larger stroke width, and set the onclick on that.

这篇关于JavaScript和SVG:如何增加onClick事件的可点击区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆