SVG - polygon hover does not work correclty [duplicate]

梦想的初衷 提交于 2019-12-14 01:26:31

问题


As you can see on gif below, :hover state does not work properly when I move the mouse cursor from bottom polygon to upper polygon:

[jsfiddle]

Tested in Chrome and Firefox - the result is the same.

How can I achieve SVG polygon turn :hover state on right after mouse cursor enters its borders?


回答1:


There is no fill to catch the pointer event so it fails.

A simple transparent fill corrects it.

polygon {
  stroke-width: 1;
  stroke: red;
  fill: transparent;
}
polygon:hover {
  fill: red;
}
<svg viewBox="0 0 999 799">
  <polygon points="445,161 345,174 500,10" />

  <polygon points="445,161 345,174 500,270" />
</svg>

As mentioned by Robert Longson

pointer-events: visible is the preferred and performant solution.

polygon {
  stroke-width: 1;
  stroke: red;
  fill: none;
  pointer-events: visible;
}
polygon:hover {
  fill: red;
}
<svg viewBox="0 0 999 799">
  <polygon points="445,161 345,174 500,10" />

  <polygon points="445,161 345,174 500,270" />
</svg>


来源:https://stackoverflow.com/questions/36137604/svg-polygon-hover-does-not-work-correclty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!