我正在尝试使用JavaScript和CSS过渡来动画化各种SVG形状的属性。
例如:
HTML:
JavaScript:
CSS:
这里有一个完整的JSFiddle:http://jsfiddle.net/kkhvzyjq/ 在Chrome中,这个代码可以很好地运行。然而,在Safari和Firefox中,虽然新的属性得到应用(形状移动),但没有过渡/动画。
是否有一种方法可以在这些浏览器中实现这个效果?
例如:
HTML:
<svg width="400" height="400">
<circle id="circle" cx="200" cy="200" r="15" stroke="none" fill="blue" />
<rect id="rect" x="100" y="100" height="30" width="30" stroke="none" fill="red" />
</svg>
<button id="button">Animate</button>
JavaScript:
document.getElementById("button").addEventListener("click", function () {
var circle = document.getElementById("circle");
var cx = 50 + Math.round(Math.random() * 300);
var cy = 50 + Math.round(Math.random() * 300);
// using 'setAttribute'
circle.setAttribute("cx", cx);
circle.setAttribute("cy", cy);
var rect = document.getElementById("rect");
var x = 50 + Math.round(Math.random() * 300);
var y = 50 + Math.round(Math.random() * 300);
// using 'setAttributeNS'
rect.setAttributeNS(null, "x", x);
rect.setAttributeNS(null, "y", y);
}, false);
CSS:
circle, rect {
-webkit-transition: all 0.7s ease-in-out;
-moz-transition: all 0.7s ease-in-out;
transition: all 0.7s ease-in-out;
}
这里有一个完整的JSFiddle:http://jsfiddle.net/kkhvzyjq/ 在Chrome中,这个代码可以很好地运行。然而,在Safari和Firefox中,虽然新的属性得到应用(形状移动),但没有过渡/动画。
是否有一种方法可以在这些浏览器中实现这个效果?