如何使用onmouseover功能突出显示SVG图像的一部分?

时间:2019-04-02 00:05:45

标签: javascript css svg

我有一张欧洲地图的SVG图像,每个国家的分区都用路径表示。如果将鼠标悬停在某个国家/地区上,我希望将悬停的国家/地区的不透明度更改为0.5。我尝试定义要调用的JavaScript函数,但是当我将鼠标悬停在某个国家时,似乎什么也没发生。这是我的SVG代码供参考,以及我尝试编写的JavaScript:

<script
    xlink:href="../map.js"
    id="script99"
    type="text/javascript" />
 <g
    inkscape:groupmode="layer"
    id="layer2"
    class="section"
    inkscape:label="paths"
    transform="translate(0,-230.143)">
   <path
      style="fill:#3399ff;stroke:#000000;stroke-width:0.06832593px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;"
      d="m 170.10418,253.29039 0.16365,-1.29169 2.4003,-0.68196 2.91852,-0.51346 0.90921,0.17651 0.12727,0.61776 -0.50914,0.82636 -0.14547,1.3639 -1.51836,1.04298 -0.82736,0.85845 c 0,0 -0.28187,0.48137 -0.33642,0.48939 -0.0545,0.007 -0.99103,-0.7381 -0.99103,-0.7381 l -0.66371,-0.43325 -0.60915,-1.37191 z"
      id="ukraine" 
      onclick="Here(id)"
      onmouseover="MouseOver(this)"
      inkscape:connector-curvature="0"
      /> 
function MouseOver(elem){
    elem.opacity = 0.1;
}

1 个答案:

答案 0 :(得分:1)

我已经删除了内联样式,并将它们放入了<style>标签中。您可能会看到<style>标签位于svg元素内。

为您添加的样式

#ukraine:hover {
      fill-opacity: 0.5;
    }

这是演示:

<svg viewBox="170 250 7 7">
  <style type="text/css">
        <![CDATA[
        #ukraine {
  fill: #3399ff;
  fill-opacity: 1;
  stroke: #000000;
  stroke-width: 0.06832593px;
  stroke-linecap: butt;
  stroke-linejoin: miter;
  stroke-opacity: 1;
}

#ukraine:hover {
  fill-opacity: 0.5;
}
        ]]> 
  </style>
  
   <path id="ukraine"
      d="m 170.10418,253.29039 0.16365,-1.29169 2.4003,-0.68196 2.91852,-0.51346 0.90921,0.17651 0.12727,0.61776 -0.50914,0.82636 -0.14547,1.3639 -1.51836,1.04298 -0.82736,0.85845 c 0,0 -0.28187,0.48137 -0.33642,0.48939 -0.0545,0.007 -0.99103,-0.7381 -0.99103,-0.7381 l -0.66371,-0.43325 -0.60915,-1.37191 z"
       
      /> 
</svg>