2016-08-23 60 views
1

我一直在研究一個網站,該網站要求通過JavaScript將內聯樣式設置爲元素。樣式必須內聯,因爲它們需要設置的值在JavaScript中確定。 到目前爲止,這個代碼已經在大多數瀏覽器工作(Gears.leftBot指元素):使用JavaScript設置內聯樣式不工作

Gears.leftBot.style = "transform: rotate(20deg);"; 

然而,在微軟的邊緣,這個代碼什麼也不做。我甚至直接在控制檯中運行這一行代碼,但沒有結果(能夠確認它確實將Gears.leftBot識別爲元素)。我能做些什麼來解決這個問題?

完整的JavaScript:

var Gears = { 
    //Dependencies 
    rightTop: document.getElementById('gear-rt'), 
    rightMid: document.getElementById('gear-rm'), 
    rightBot: document.getElementById('gear-rb'), 
    leftTop: document.getElementById('gear-lt'), 
    leftBot: document.getElementById('gear-lb'), 

    init : function(){ 
     Gears.scrollAnima(); 
    }, 

    scrollAnima : function(){ 
     var pos = window.pageYOffset; 
     var bigGearDeg = pos/1.389; 
     var midGearDeg = pos * 1.2; 
     var litGearDeg = pos * 1.44; 
     Gears.rightTop.style = "transform: rotate(" + bigGearDeg + "deg);"; 
     Gears.rightMid.style = "transform: rotate(-" + midGearDeg + "deg);"; 
     Gears.rightBot.style = "transform: rotate(" + litGearDeg + "deg);"; 
     Gears.leftTop.style = "transform: rotate(" + litGearDeg + "deg);"; 
     Gears.leftBot.style = "transform: rotate(-" + bigGearDeg + "deg);"; 
     setTimeout(function(){Gears.scrollAnima()}, 15); 
    } 
}; 

Gears.init(); 
+0

側面說明,使用requestAnimationFrame代替的setTimeout – Sampson

回答

1

嘗試:

Gears.leftBot.style.transform = 'rotate(20deg)' 

或:

Gears.leftBot.setAttribute('style', 'transform: rotate(20deg);') 
相關問題