關(guān)于設(shè)置obj的opacity,一直失敗
問(wèn)題描述
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) } 想要通過(guò)傳進(jìn)去一個(gè)對(duì)象設(shè)置它的opacity,結(jié)果一直失敗.經(jīng)過(guò)調(diào)試發(fā)現(xiàn)只有第一次的時(shí)候能夠成功設(shè)置,之后每次進(jìn)else設(shè)置之后對(duì)象的opacity的值并沒(méi)有改變.是為什么呢?
問(wèn)題解答
回答1:obj.style.opacity 的獲取值是字符串,而非數(shù)字, 所以+號(hào)操作得不到你想要的值, parseFloat一下即可
相關(guān)文章:
