我如何在悬停在另一个元素上时更改一个 SVG 路径的 ID 元素

huangapple go评论47阅读模式
英文:

How can I change one svg path ID element when hovering over another element

问题

我为SVG路径元素分配了ID属性,并将它们命名为opacity-1n和opacity-1e。我希望当鼠标悬停在opacity-1n上时,opacity-1e会出现。
英文:

I assigned SVG path elements with an ID property and named them opacity-1n and opacity-1e. I want to hover the mouse over opacity-1n, then opacity-1e will appear.

<g id="Layer_1">
<path id="opacity-1n" d="M135.4 6.8v38.3h-1.9l-15.1-13.8-1.8-1.6-6.8-6.2-2-1.8v23.4h-7.7V6.7h1.9L119.1 22l1.9 1.7 6.7 5.9V6.8z" class="st0"/>
<path id="opacity-1e" d="M196.9 22.9h18.2v6.3h-18.2v9.1H219v6.8h-29.8V6.7H219v6.8h-22.1z" class="st0"/>
 </g>
#opacity-1n:hover #opacity-1e {opacity: 1;}

#opacity-1e {opacity: 0;}

Also tried:

#opacity-1n:hover + #opacity-1e {opacity: 1;}

#opacity-1n:hover > #opacity-1e {opacity: 1;}

#opacity-1n:hover ~ #opacity-1e {opacity: 1;}

答案1

得分: 2

你只需隐藏第二个 path 元素。

#opacity-1n:hover + #opacity-1e {
  opacity: 1;
}

#opacity-1e {
  opacity: 0;
}
<svg>
  <g id="Layer_1">
    <path id="opacity-1n" d="M135.4 6.8v38.3h-1.9l-15.1-13.8-1.8-1.6-6.8-6.2-2-1.8v23.4h-7.7V6.7h1.9L119.1 22l1.9 1.7 6.7 5.9V6.8z" class="st0"/>
    <path id="opacity-1e" d="M196.9 22.9h18.2v6.3h-18.2v9.1H219v6.8h-29.8V6.7H219v6.8h-22.1z" class="st0"/>
  </g>
</svg>
英文:

You just need to hide the second path-element.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#opacity-1n:hover + #opacity-1e {
  opacity: 1;
}

#opacity-1e {
  opacity: 0;
}

<!-- language: lang-html -->

&lt;svg&gt;
  &lt;g id=&quot;Layer_1&quot;&gt;
    &lt;path id=&quot;opacity-1n&quot; d=&quot;M135.4 6.8v38.3h-1.9l-15.1-13.8-1.8-1.6-6.8-6.2-2-1.8v23.4h-7.7V6.7h1.9L119.1 22l1.9 1.7 6.7 5.9V6.8z&quot; class=&quot;st0&quot;/&gt;
    &lt;path id=&quot;opacity-1e&quot; d=&quot;M196.9 22.9h18.2v6.3h-18.2v9.1H219v6.8h-29.8V6.7H219v6.8h-22.1z&quot; class=&quot;st0&quot;/&gt;
 &lt;/g&gt;
&lt;/svg&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月18日 01:29:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定