I was looking at the NYTimes interactive on state subsidies this morning and noticed that even when a state is obscured by a dot it is brought forward on hover.
For example the dot covering Massachusetts also partially covers New Hampshire, yet when you when you mouse into the covered part of New Hampshire, New Hampshire is brought forward.
How do you suppose they achieve this? The dots are in front of the state outlines based on their order in the DOM. I thought there might be a second set of state outlines on top of everything, listening for mouseovers that would trigger the underlying shape, but that doesn't seem to be the case.
I need to implement similar functionality in an application I'm working on and am curious about an elegant way with SVG elements.
Thanks.
I was looking at the NYTimes interactive on state subsidies this morning and noticed that even when a state is obscured by a dot it is brought forward on hover.
For example the dot covering Massachusetts also partially covers New Hampshire, yet when you when you mouse into the covered part of New Hampshire, New Hampshire is brought forward.
How do you suppose they achieve this? The dots are in front of the state outlines based on their order in the DOM. I thought there might be a second set of state outlines on top of everything, listening for mouseovers that would trigger the underlying shape, but that doesn't seem to be the case.
I need to implement similar functionality in an application I'm working on and am curious about an elegant way with SVG elements.
Thanks.
Share edited Dec 5, 2012 at 14:19 seliopou 2,91622 silver badges19 bronze badges asked Dec 4, 2012 at 15:07 Al R.Al R. 2,4804 gold badges30 silver badges40 bronze badges 3-
3
This isn't as plicated as it may seem, just give your dots a css property of
pointer-events: none;
. This will stop the mouse knowing they exist, so you can hover over what is behind them – Andy Commented Dec 4, 2012 at 15:10 - Yes. Only thing is that IE doesn't support this css prop. Even IE9. stackoverflow./questions/5855135/… – meetamit Commented Dec 4, 2012 at 18:40
-
@meetamit:
pointer-events: none
is supported on SVG elements in IE9. – methodofaction Commented Dec 4, 2012 at 21:30
3 Answers
Reset to default 4As noted by Andy the circles in that NYT graphic have a CSS pointer-events property of none:
circle {
pointer-events: none;
}
the bring to front behavior can be achieved in a the mouseover function using this method:
stateShape.on("mouseover",function(){this.parentNode.appendChild(this);})
Now you can just use the raise()
method:
Example in a function:
highlightSite(site) {
let selectedSite = d3.selectAll(`[siteName=${site}]`)
selectedSite.raise()
selectedSite
.attr('stroke', 'black')
.style('opacity', 1)
}
Another technique is to ensure the element being hovered over is always the last element in the SVG therefore it will be to the front.
function onMouseEnter(e) {
e.currentTarget.remove();
document.querySelector('svg.map').appendChild(e.currentTarget);
}
with
svg.map path:hover {
stroke: var(--dark);
stroke-width: 2px;
cursor: pointer;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742292563a4416473.html
评论列表(0条)