【发布时间】:2017-10-29 03:05:55
【问题描述】:
只要 SVG 没有 z-index,我就可以通过 DOM 操作使我的矩形改变 z-position。但是,我有一个新问题。由于某种原因,过渡停止工作。我怀疑这是因为 DOM 重新排序,但我必须这样使用它。
function redZ() {
document.getElementById('svgField').appendChild(document.getElementById('redRect'));
document.getElementById('redRect').style.transition = "2s linear";
document.getElementById('redRect').style.fill = "black";
}
function blueZ() {
document.getElementById('svgField').appendChild(document.getElementById('blueRect'));
document.getElementById('blueRect').style.transition = "2s linear";
document.getElementById('blueRect').style.fill = "yellow";
}
<svg width="180" height="150" id="svgField">
<rect onclick="redZ()" id="redRect" x="0" y="0" width="100" height="100" fill="red" />
<rect onclick="blueZ()" id="blueRect" x="60" y="40" width="100" height="100" fill="blue" />
</svg>
【问题讨论】:
-
根据实际 SVG 的整体结构,您可以选择将 SVG 中的元素拆分为单独的
<svg>元素,然后在这些元素上应用z-index。
标签: javascript svg