我有一個需要旋轉元素的SVG動畫。由於IE不支持它們,並且beginElement()屬性在IE或Firefox中也不受支持,所以SMIL和基於css的動畫不存在問題。 IE的大小調整不是問題,因爲CSS修復得很好。IE瀏覽器(全部)的SVG旋轉失敗
問題在於它不是一個標準形狀(矩形,圓形等),所以svg動畫屬性會以驚人的方式失敗,因爲默認情況下沒有中心點 - getBBox()是首選方法。
我有一個plunk在html中的svg的例子。爲了便於攜帶,腳本必須位於svg中,並且我已經在Chrome,Firefox和Opera中使用它,但IE無法旋轉元素,雖然沒有顯示錯誤。 JS如下:
var svgNS = "http://www.w3.org/2000/svg";
function init(evt) {
addRotateTransform('spinner', 1.5, 1);
}
function addRotateTransform(target_id, dur, dir) {
var my_element = document.getElementById(target_id);
var a = document.createElementNS(svgNS, "animateTransform");
var bb = my_element.getBBox();
var cx = bb.x + bb.width/2;
var cy = bb.y + bb.height/2;
a.setAttributeNS(null, "attributeName", "transform");
a.setAttributeNS(null, "attributeType", "XML");
a.setAttributeNS(null, "type", "rotate");
a.setAttributeNS(null, "dur", dur + "s");
a.setAttributeNS(null, "repeatCount", "indefinite");
a.setAttributeNS(null, "from", "0 "+cx+" "+cy);
a.setAttributeNS(null, "to", 360*dir+" "+cx+" "+cy);
my_element.appendChild(a);
try {
a.beginElement(); // this works in Chrome
}
catch(err) {
window.setTimeout(init, 0); // this works in FF
}
}
IE不支持SMIL。你可以試試fakeSmile(https://leunen.me/fakesmile/) –
感謝Robert,使用另一個庫在這裏沒有幫助,因爲它需要被封裝爲thingemebob,而fakeSmile目前還不支持旋轉。 –
您需要使用純JavaScript並操作DOM。 –