SVG CSS 过渡不适用于“use”元素 (Chrome)
SVG CSS transition not working on `use` element (Chrome)
我无法获得 CSS 过渡来处理 SVG 内的 use
元素。
它在原始 SVG 上有效,但在我使用 use
时无效。见下文。
这似乎是 Chrome 具体的(两个示例都适用于 FF)。
.stretched-rect {
width: 50px;
height: 50px;
}
.stretched-rect svg {
width: 100%;
height: 100%;
}
.stretched-rect {
--fill-color: red;
}
.stretched-rect:hover {
--fill-color: blue;
}
Example with original svg (this works):
<div class="stretched-rect">
<svg viewbox="0 0 100 100" preserveAspectRatio="none">
<rect width="100" height="100" rx="15" style="fill: var(--fill-color); transition: 1s fill;"/>
</svg>
</div>
<br><br>
<!-- Define SVG, so we can use it with `use` -->
<svg style="display: none;">
<symbol id="svg-rect" viewbox="0 0 100 100" preserveAspectRatio="none">
<rect width="100" height="100" rx="15" style="fill: var(--fill-color); transition: 1s fill;"/>
</symbol>
</svg>
Example using "use" (this does not work):
<div class="stretched-rect">
<svg><use xlink:href="#svg-rect"/></svg>
</div>
如其他人所述,这似乎是 Chrome 中的错误。请参阅下面的解决方法。
正如我评论的那样:您可以将样式应用于 use 元素,而不是在符号中设置矩形样式:
从符号的矩形中删除样式并将其粘贴到使用元素:
.stretched-rect {
width: 50px;
height: 50px;
}
.stretched-rect svg {
width: 100%;
height: 100%;
}
.stretched-rect {
--fill-color: red;
}
.stretched-rect:hover {
--fill-color: blue;
}
Example with original svg (this works):
<div class="stretched-rect">
<svg viewbox="0 0 100 100" preserveAspectRatio="none">
<rect width="100" height="100" rx="15" style="fill: var(--fill-color); transition: 1s fill;"/>
</svg>
</div>
<br><br>
<!-- Define SVG, so we can use it with `use` -->
<svg style="display: none;">
<symbol id="svg-rect" viewbox="0 0 100 100" preserveAspectRatio="none">
<rect width="100" height="100" rx="15" />
</symbol>
</svg>
Example using "use" (this is working now):
<div class="stretched-rect">
<svg><use xlink:href="#svg-rect" style="fill: var(--fill-color); transition: 1s fill;"/></svg>
</div>
我无法获得 CSS 过渡来处理 SVG 内的 use
元素。
它在原始 SVG 上有效,但在我使用 use
时无效。见下文。
这似乎是 Chrome 具体的(两个示例都适用于 FF)。
.stretched-rect {
width: 50px;
height: 50px;
}
.stretched-rect svg {
width: 100%;
height: 100%;
}
.stretched-rect {
--fill-color: red;
}
.stretched-rect:hover {
--fill-color: blue;
}
Example with original svg (this works):
<div class="stretched-rect">
<svg viewbox="0 0 100 100" preserveAspectRatio="none">
<rect width="100" height="100" rx="15" style="fill: var(--fill-color); transition: 1s fill;"/>
</svg>
</div>
<br><br>
<!-- Define SVG, so we can use it with `use` -->
<svg style="display: none;">
<symbol id="svg-rect" viewbox="0 0 100 100" preserveAspectRatio="none">
<rect width="100" height="100" rx="15" style="fill: var(--fill-color); transition: 1s fill;"/>
</symbol>
</svg>
Example using "use" (this does not work):
<div class="stretched-rect">
<svg><use xlink:href="#svg-rect"/></svg>
</div>
如其他人所述,这似乎是 Chrome 中的错误。请参阅下面的解决方法。
正如我评论的那样:您可以将样式应用于 use 元素,而不是在符号中设置矩形样式: 从符号的矩形中删除样式并将其粘贴到使用元素:
.stretched-rect {
width: 50px;
height: 50px;
}
.stretched-rect svg {
width: 100%;
height: 100%;
}
.stretched-rect {
--fill-color: red;
}
.stretched-rect:hover {
--fill-color: blue;
}
Example with original svg (this works):
<div class="stretched-rect">
<svg viewbox="0 0 100 100" preserveAspectRatio="none">
<rect width="100" height="100" rx="15" style="fill: var(--fill-color); transition: 1s fill;"/>
</svg>
</div>
<br><br>
<!-- Define SVG, so we can use it with `use` -->
<svg style="display: none;">
<symbol id="svg-rect" viewbox="0 0 100 100" preserveAspectRatio="none">
<rect width="100" height="100" rx="15" />
</symbol>
</svg>
Example using "use" (this is working now):
<div class="stretched-rect">
<svg><use xlink:href="#svg-rect" style="fill: var(--fill-color); transition: 1s fill;"/></svg>
</div>