gpt4 book ai didi

svg - 在 d3.js 中操纵鼠标悬停 "hit area"

转载 作者:行者123 更新时间:2023-12-04 23:20:55 25 4
gpt4 key购买 nike

我要 显示 隐藏 SVG 中的一个节点,当 鼠标悬停/ mouseout ,问题是我在节点内的形状是一条只有1.5px宽度的路径,所以不容易在鼠标悬停事件中击中该区域,这绝对不利于用户体验。

我想知道是否有办法做到这一点 命中区域更宽 使用任意宽度,但对用户不可见?

我的代码片段:

link.enter()
.append('g').attr('class', 'link')
.append('line')
.attr('class', 'path')
.attr('marker-end', function(d, i) {
if (i === 2) {
return 'url(#arrow)';
} else {
return null;
}
}).on('mouseover', function(d) {
//alert(JSON.stringify(d));
alert('mouseover');
}).on('mouseout', function(d) {
alert('mouseout');
});

css:
.node .path {
stroke: #878f8f;
stroke-width: 1.5px;
fill:none;
}

最佳答案

您可以再添加一个 lineg笔画透明,笔画宽度大,会增加命中区域。

// Subscribe to mouse events on the entire g
gEnter = link.enter()
.append('g')
.attr('class', 'link')
.on('mouseover', function(d) {
//alert(JSON.stringify(d));
alert('mouseover');
}).on('mouseout', function(d) {
alert('mouseout');
});

// Instead of 1 line, append 2 lines inside the g, and make
// one of them transparent and "fat", which will enlarge the
// hit area
lines = gEnter
.selectAll('.path').data(['visible', 'invisible'])
lines.enter()
.append('line')
.attr('class', 'path')
.attr('marker-end', function(d, i, j) {
// j is the parent's i
if (j === 2) {
return 'url(#arrow)';
} else {
return null;
}
})
.attr({
// returning null from these functions simply defaults to whatever the
// .path class's CSS props are doing
'stroke-width': function(d, i) { return d == 'invisible' ? 10 : null },
'stroke': function(d, i) { return d == 'invisible' ? 'transparent' : null }
})

关于svg - 在 d3.js 中操纵鼠标悬停 "hit area",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27238845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com