【问题标题】:SVG path area overlapSVG 路径区域重叠
【发布时间】:2014-03-03 04:00:30
【问题描述】:

我今天刚开始使用 SVG,但我不知道如何在没有悬停区域重叠问题的情况下使用非矩形形状进行悬停,正如您在 my fiddle 中看到的那样。

如果有人能指出我缺少的知识,我将不胜感激。

<svg height="444" width="257" class="svg svg1">
  <path d="M0 148 L257 0 L257 297 L0 444 Z" style="fill:lime;stroke:none;"  
      onmouseover="document.getElementById('fade1').style.opacity = '.9';" 
      onmouseout="document.getElementById('fade1').style.opacity = '0';"/>
</svg>  
<svg height="444" width="257" class="svg svg2">
  <path d="M0 148 L257 0 L257 297 L0 444 Z" style="fill:lime;stroke:none;"  
      onmouseover="document.getElementById('fade2').style.opacity = '.9';" 
      onmouseout="document.getElementById('fade2').style.opacity = '0';"/>
</svg>  
<svg height="444" width="257" class="svg svg3">
  <path d="M0 148 L257 0 L257 297 L0 444 Z" style="fill:lime;stroke:none;"  
      onmouseover="document.getElementById('fade3').style.opacity = '.9';" 
      onmouseout="document.getElementById('fade3').style.opacity = '0';"/>
</svg>

【问题讨论】:

    标签: svg hover overlap


    【解决方案1】:

    您必须将所有路径放在同一个 SVG 中,才能使悬停效果按您的意愿工作。这是由于 SVG 元素(而不是路径)相互重叠,Z-indexes 不能像您希望的那样工作

    话虽如此,你不能很容易地定位&lt;path&gt;s,所以我使用this tool 来应用你拥有的样式。或者,您可以将它们放在单独的 &lt;g&gt; 元素中并以这种方式定位它们

    要注意的另一件事是您应该将:hover 效果应用于路径,而不是 SVG 元素

    /* SVG */
    <svg height="1150" width="257">
        <path d="M0 244L257 96L257 393L0 540z" style="fill:lime;stroke:none;"/>
        <path d="M0 545L257 397L257 694L0 841z" style="fill:lime;stroke:none;"/>
        <path d="M0 846L257 698L257 995L0 1142z" style="fill:lime;stroke:none;"/>
    </svg>
    
    /* CSS */
    svg {
        margin:0 auto;
        display:block;
        width:257px;
    }
    svg path {
        opacity:.3;
    }
    svg path:hover {
        opacity:1;
    }
    

    Demo

    附:我假设 javascripg onmouseovers 和 onmouseouts 来自失败的尝试?

    【讨论】:

    • 当我将鼠标悬停在相应的路径上时,我正在使用 onmouseovers 和 onmouseouts 来更改带有淡入淡出 ID 的图像的不透明度。我会试试你告诉我的,然后告诉你效果如何。非常感谢。
    • @user3372093 如果您发现某个问题对您的问题感到满意,您可以通过单击问题旁边的复选标记将其标记为正确,然后会给出答案和 OP (原始海报 - 你)这样做的声誉
    • 耶!!有效。如果您愿意,可以查看结果。 wolfsbrain.com/welcome.html
    • @user3372093 我喜欢!您可能会尝试延迟过渡,以便可以更清晰地看到图像,但这只是一个想法
    • 或者只是淡出而不是跳跃
    猜你喜欢
    • 2015-10-29
    • 2016-04-02
    • 2014-12-20
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多