javascript - Styling SVG with CSS and JS -


i have image generated inkscape, need change each of elements background color on hover , add/remove classes on click... has accomplished either using js/jquery or there advice make work?.

<?xml version="1.0" encoding="utf-8" standalone="no"?> <!-- created inkscape (http://www.inkscape.org/) -->  <svg    xmlns:dc="http://purl.org/dc/elements/1.1/"    xmlns:cc="http://creativecommons.org/ns#"    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"    xmlns:svg="http://www.w3.org/2000/svg"    xmlns="http://www.w3.org/2000/svg"    xmlns:sodipodi="http://sodipodi.sourceforge.net/dtd/sodipodi-0.dtd"    xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"    width="216.14172"    height="216.14172"    id="svg2"    version="1.1"    inkscape:version="0.48.4 r9939"    sodipodi:docname="cuadrado.svg">   <defs      id="defs4" />   <sodipodi:namedview      id="base"      pagecolor="#ffffff"      bordercolor="#666666"      borderopacity="1.0"      inkscape:pageopacity="0.0"      inkscape:pageshadow="2"      inkscape:zoom="1.15"      inkscape:cx="350"      inkscape:cy="142.29259"      inkscape:document-units="px"      inkscape:current-layer="layer1"      showgrid="false"      inkscape:window-width="1278"      inkscape:window-height="768"      inkscape:window-x="0"      inkscape:window-y="0"      inkscape:window-maximized="1"      width="1052.36px"      units="cm"      showguides="true"      inkscape:guide-bbox="true" />   <metadata      id="metadata7">     <rdf:rdf>       <cc:work          rdf:about="">         <dc:format>image/svg+xml</dc:format>         <dc:type            rdf:resource="http://purl.org/dc/dcmitype/stillimage" />         <dc:title />       </cc:work>     </rdf:rdf>   </metadata>   <g      inkscape:label="capa 1"      inkscape:groupmode="layer"      id="layer1"      transform="translate(0,-836.22106)">     <rect        style="fill:none;stroke:#000000;stroke-width:0.74252009;stroke-opacity:1"        id="center"        width="105.19868"        height="105.27945"        x="55.705582"        y="891.74548" />     <path        style="fill:none;stroke:#000000;stroke-width:0.74252009;stroke-opacity:1"        d="m 55.705578,996.86712 105.198682,0 52.9246,53.28568 -211.693293,0 z"        id="rect3319-7-9"        inkscape:connector-curvature="0"        sodipodi:nodetypes="ccccc" />     <path        style="fill:none;stroke:#000000;stroke-width:0.74252009;stroke-opacity:1"        d="m 213.99147,838.78016 -0.64542,210.56404 -51.95391,-52.31936 0,-105.27946 z"        id="rect3319-7-0"        inkscape:connector-curvature="0"        sodipodi:nodetypes="ccccc" />     <path        style="fill:none;stroke:#000000;stroke-width:0.74252009;stroke-opacity:1"        d="m 55.540408,892.31605 0,105.27947 l 2.94107,1050.5608 3.586495,838.70491 z"        id="rect3319-7-0-3"        inkscape:connector-curvature="0"        sodipodi:nodetypes="ccccc" />     <path        onmouseout="evt.target.setattribute('fill','none)"        onmouseover="evt.target.setattribute('fill','yellow')"        inkscape:label="#rect3319-7"        sodipodi:nodetypes="ccccc"        inkscape:connector-curvature="0"        id="path4286"        d="m 3.4264127,839.33911 210.4024473,0 -52.9246,52.63972 -105.198674,0 z"        style="fill:none;stroke:#000000;stroke-width:0.74252009;stroke-opacity:1" />   </g> </svg> 

edit

so far have tried change styling hover event in external css like:

#center.hover{background-color:#f0f0f0} 

after adding css declaration, of course

 <defs id="defs4" >       <link href="style.css" type="text/css" rel="stylesheet"        xmlns="http://www.w3.org/1999/xhtml"/>    </defs> 

but doesn't work, tried jquery.svg , doesn't work, image looks plain on browser , events seem ignored, first time working svg.

you can use pseudo-element :hover on svg elements:

#path4286:hover {     fill:yellow; } 

but have remove inline styles tags, since have precedence on styles applied in stylesheet (unless don't care using '!important' time).

you can remove elements tree using dom (calling removechild parent node) can obtain same visual effect using css display:none below:

var path4286 = document.getelementbyid("path4286"); path4286.addeventlistener("click", function() {     path4286.style.display = 'none'; }); 

see: jsfiddle


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -