一般来说,答案是否定的。克隆的元素并不独立于它们所引用的元素。恰恰相反,元素的渲染内容<use>
必须反映引用元素的所有生活变化,包括所有动画。
对于 SMIL 和 CSS 动画,答案仍然正确。现在您可能知道,如果元素具有可以被该内容继承的样式规则,则呈现的内容可能会有所不同。<use>
(fill
如下例所示。)那么,如果您在引用的源元素上定义 CSS 动画,然后animation-delay
为每个设置不同的样式规则,会发生什么<use>
?动画仍将同时开始,animation-delay
不可继承。
对于您描述的动画,使用 CSS 动画有一个不错的解决方案。你的克隆元素是惰性的,只需要作为一个整体移动。因此,对<use>
元素本身进行动画处理就足够了。现在上面暗示的技术起作用了:
.dropping {
animation: drop 2s forwards;
}
@keyframes drop {
from {
transform: translate(0px, 0px);
}
to {
transform: translate(0px, 300px);
}
}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="250" height="300">
<symbol id="drop">
<rect width="50" height="50" />
</symbol>
<use class="dropping" xlink:href="#drop" x="0" style="animation-delay:0s;fill:rgb(255, 0, 0);" />
<use class="dropping" xlink:href="#drop" x="50" style="animation-delay:0.5s;fill:orange;" />
<use class="dropping" xlink:href="#drop" x="100" style="animation-delay:1s;fill:yellow;" />
<use class="dropping" xlink:href="#drop" x="150" style="animation-delay:1.5s;fill:green;" />
<use class="dropping" xlink:href="#drop" x="200" style="animation-delay:2s;fill:blue;" />
</svg>