Hover effect on SVG group elements
NickName:Andy Place Ask DateTime:2013-10-09T01:35:21

Hover effect on SVG group elements

Please excuse my ignorance, SVG is very new to me. I'm trying to get a hover effect on a group of elements in my inline SVG. Each group of elements is a closely positioned set of circles. I can achieve this hover effect with css, but obviously this will only work when the mouse is positioned over a circle. What I want is the effect to work when the mouse is over the whole area that contains the circles, so spaces and circles combined.

<style>
svg {
  height: 220px;
  width: 400px;
  background: grey;
}

.na circle,
.as circle {
  fill: white;
}

.na:hover circle {
  fill: blue;
}
</style>

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <g class="na">
    <circle cx="35%" cy="2.85%" r="2.8" />
    <circle cx="36.75%" cy="2.85%" r="2.8" />
    .
    .
    <circle cx="38.5%" cy="8.55%" r="2.8" />
    <circle cx="40.25%" cy="8.55%" r="2.8" />
  </g>    
</svg>

http://jsfiddle.net/c3EaX/

See when moving the mouse over a group the hover flickers as you go between circle and space between them.

How would you go about getting the mouse over effect to appear to cover the whole area covered by the group? Is there an SVG element that can be used for this?

Copyright Notice:Content Author:「Andy Place」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/19254520/hover-effect-on-svg-group-elements

Answers
Alexandr_TT 2019-07-15T15:11:19

Use pattern\n\nFor the solution, you can use pattern which is based on a single circle.\n\nIn this case, you do not need to draw a lot of circles to fill the shape.\n\nDifficulties arise when coloring clones of a circle when filling the figure of the SVG pattern.\n\nBut it is possible to solve the problem by animating the coloring of the circles inside the pattern. \n\n<animate attributeName=\"fill\" values=\"white;dodgerblue\" begin=\"path1.mouseover\" dur=\"0.1s\" fill=\"freeze\" /> \n\n\r\n\r\n<style>\r\n#ptn1 {\r\nfill:dodgerblue;\r\n}\r\n</style>\r\n<svg id=\"svg1\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" \r\n xmlns:xlink=\"http://www.w3.org/1999/xlink\"\r\n\t width=\"200\" height=\"200\" viewBox=\"0 0 100 100\" > \r\n<defs>\r\n<pattern id=\"ptn1\" \r\n x=\"0\" y=\"0\" width=\"5\" height=\"5\"\r\n patternUnits=\"userSpaceOnUse\">\r\n<circle cx=\"3\" cy=\"3\" r=\"2\" fill=\"none\" stroke=\"grey\" stroke-width=\"0.5\" >\r\n <animate attributeName=\"fill\" values=\"white;dodgerblue\" begin=\"path1.mouseover\" dur=\"0.3s\" fill=\"freeze\" />\r\n <animate attributeName=\"fill\" values=\"dodgerblue;white\" begin=\"path1.mouseout\" dur=\"0.3s\" fill=\"freeze\" />\r\n </circle>\r\n</pattern>\r\n</defs>\t\t \r\n\r\n<path id=\"path1\" d=\"m10 50 v-5h5 v-5h5 v-5h5 v-5h5 v-5h5 v5h5 v5h5 v5h5 v5h5 v5h-45 \" stroke=\"none\" fill=\"url(#ptn1)\" />\r\n</svg>",


Matt W 2017-10-03T15:39:18

The accepted answer didn't work for me. I found the following solution:\n\ng {\n pointer-events: bounding-box;\n opacity: 0.4;\n}\ng:hover {\n opacity: 1;\n}\n",


vals 2013-10-08T18:10:19

You need to put something to cover the missing area.\n\nThe easier way would be this one:\n\n.na circle,\n.as circle {\n fill: white;\n stroke: transparent;\n stroke-width: 4px;\n}\n\n\nupdated fiddle",


Zach 2018-06-14T15:12:50

The answers by @vals and @M_Willett won't work in Firefox v60.0.2 in MacOs (High Sierra). I'm using:\n\ng {\n pointer-events: bounding-box;\n}\n\n\nIn Chrome and it works perfectly. Tried the answer by @vals as well and that doesn't work in Firefox either.",


More about “Hover effect on SVG group elements” related questions

Hover effect on SVG group elements

Please excuse my ignorance, SVG is very new to me. I'm trying to get a hover effect on a group of elements in my inline SVG. Each group of elements is a closely positioned set of circles. I can ach...

Show Detail

On hover effect on a SVG group

I'm working on a pretty big map with lots of different areas and text on top of them, sort of like countries. I want to add a on mouse hover effect when hovering over this area. The effect should a...

Show Detail

Hover effect on SVG

I have SVG of US map with 5 different regions created with g element. I want to change the color of the region on hover of the region but i can not get the result on hover. When i apply hover effe...

Show Detail

How to target hover effect with Snap.SVG?

I'm testing animations with Snap.svg on hover in/out and on click with an SVG illustration I created and exported with Illustrator. I have a group that includes others sub-groups. On hovering the ...

Show Detail

svg grouped elements jointly hover

Is it possible for hover on an svg element cause other elements with the same class to hover too without jQuery? Or do I have to next the two into an outer group? I have inside an inline svg the

Show Detail

Selectively apply a CSS effect on a group of SVG elements

I draw a group of 3 svg elements: a blue square, a red circle and a green ellipse (see code below). At the moment when one of these elements is mouse overed, the 3 elements scale up. I am trying to...

Show Detail

Hover states on SVG elements

Linked SVG elements don't seem to behave like linked HTML elements. If you link a circle, or a path (including text rendered as paths) you have to hold your mouse directly above the content in orde...

Show Detail

SVG weird behaviour on hover only in firefox - svg-jquery.

I was only playing around with SVG when I noticed that added a hover effect to the elements acts rather oddly but only in FE. In ie, chrome etc its all working as i would expect. In short, hoverin...

Show Detail

Apply :hover to all children of an svg group

How do I get all the elements of an svg group to change their fill color on hover? The example below does not work at all. If I use .sgroup circle:hover only the circle under the pointer works not...

Show Detail

How to give hover effect on svg in next?

I am using this right now with tailwindcss but it's not working Please suggest me any way to give hover effect on svg &lt;Image className=&quot;hover:fill-black&quot; src={IconCopy} alt=&quot;Copy_...

Show Detail