简体   繁体   English

我如何设计 SVG 样式<use>悬停元素?

[英]How i can style SVG <use> elements on hover?

I'm beginner in SVG.我是 SVG 的初学者。 I'm trying to change style of multiple <use> elements on hover at a specific <use> element with css, but I can't, because <use> elements using Shadow DOM .我正在尝试<use> css 更改悬停在特定<use>元素上的多个<use>元素的样式,但我不能,因为<use>元素使用Shadow DOM

I have the following <defs> :我有以下<defs>

<defs>
    <filter id="Sjax0b81q1" filterUnits="userSpaceOnUse">...</filter>
    <circle cx="0" cy="0" r="40" id="action-circle" style="cursor: move; fill: #fff;" filter="url('#Sjax0b81q1')" class="el action-el"></circle>
    <g id="condition-rhombus" style="cursor: move; fill: #fff;" class="el condition-el" transform="matrix(1,0,0,1,0,0)">
        <circle cx="0" cy="0" r="40" fill="none"></circle>
        <path d="M -35 0, L 0 -35, L 35 0, L 0 35 L -35 0" style="stroke-linecap: round; stroke: white;" filter="url('#Sjax0b81q1')" class="condition-rhombus"></path>
    </g>
    <g id="svg-plus-button">
        <circle cx="0" cy="40" r="10" id="svg-plus-circle" fill="none" style="fill-opacity: 1;" class="svg-plus-circle"></circle>
        <path d="M956.8,408.....408.5z" id="svg-plus-sign" fill="none" transform="matrix(0.008,0,0,0.008,-4,36)" style="pointer-events: none;" class="svg-plus-sign"></path>
    </g>
    <rect x="-20" y="-20" width="40" height="40" id="rect-active-layer" fill="none" style="pointer-events: visible;" class="rect-active-layer"></rect>
    <path d="M252.967.....2v1Z" id="api-svg" class="cls-1"></path>
</defs>

And I have a group of elements that contains several <use> elements:我有一组包含几个<use>元素的元素:

<g id="action-group-2" class="external action-group" transform="matrix(1,0,0,1,420,180)">
    <g class="internal action-group">
        <rect x="-40" y="-40" width="80" height="80" fill="none"></rect>
    </g>
    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#action-circle"></use>
    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#svg-plus-button" id="useSjax0b81q1k"></use>
    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#rect-active-layer"></use>
    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#api-svg"></use>
</g>

For example, I need to change the <path> fill in element with id #api-svg , when I hover on the #action-circle .例如,当我将鼠标悬停在#action-circle上时,我需要更改 ID 为#api-svg<path>填充元素。

How can I do this?我怎样才能做到这一点? Maybe there is another way to render and styling reusable elements on hover.也许还有另一种方法可以在悬停时渲染和设置可重用元素的样式。

Define the path to have fill="inherit" , then you should be able to set fill="whatever" on the <use> element's styles and it will work.定义具有fill="inherit"的路径,然后您应该能够在<use>元素的样式上设置fill="whatever"并且它会起作用。

 use:hover { fill: red; }
 <svg> <defs> <circle id="test" fill="inherit" cy="10" r="10" /> </defs> <use xlink:href="#test" transform="translate(10,0)" /> <use xlink:href="#test" transform="translate(30,0)" /> <use xlink:href="#test" transform="translate(50,0)" /> <text y="40">Hover over the circles!</text> </svg>

There's nothing overly complex here.这里没有什么过于复杂的。 Just change the 'use' element, rather than anything in the defs area (unless you want it to affect everything that references it).只需更改 'use' 元素,而不是 defs 区域中的任何内容(除非您希望它影响引用它的所有内容)。

You can either style the use element via normal css, via a css selector, eg it's id is possibly simplest.您可以通过普通 css 或 css 选择器设置 use 元素的样式,例如它的 id 可能是最简单的。

Or you can set the fill svg attribute on the svg 'use' element you are handling.或者您可以在您正在处理的 svg 'use' 元素上设置 fill svg 属性。

You shouldn't need fill inherit or anything unless I'm missing something.除非我遗漏了一些东西,否则您不需要填充继承或任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM