文章詳情頁
關(guān)于設(shè)置obj的opacity,一直失敗
瀏覽:126日期:2024-05-25 16:27:56
問題描述
var timer = null; function touming(obj,num){clearInterval(timer);timer = setInterval(function(){ var speed = (num - obj.style.opacity)*100/5; speed = speed>0 ? Math.ceil(speed) : Math.floor(speed); if(obj.style.opacity == num){clearInterval(timer); }else{obj.style.opacity = obj.style.opacity + speed/100; }},100) } 想要通過傳進去一個對象設(shè)置它的opacity,結(jié)果一直失敗.經(jīng)過調(diào)試發(fā)現(xiàn)只有第一次的時候能夠成功設(shè)置,之后每次進else設(shè)置之后對象的opacity的值并沒有改變.是為什么呢?
問題解答
回答1:obj.style.opacity 的獲取值是字符串,而非數(shù)字, 所以+號操作得不到你想要的值, parseFloat一下即可
相關(guān)文章:
排行榜

熱門標簽