同時運動的寬度問題
??function?startMove(obj,json,fn){//使用json以同時傳多個屬性和目標值 clearInterval(obj.timer); obj.timer=setInterval(function(){ for(var?attr?in?json){ var?icur=0; if(attr=='opacity'){ icur=Math.round(parseFloat(getStyle(obj,attr))*100); }else{ icur=parseInt(getStyle(obj,attr)); } var?speed=(json[attr]-icur)/8; speed=speed>0?Math.ceil(speed):Math.floor(speed);//由于speed精度不夠,導致數值較大時會導致運動的結果打不到預期值??? if(icur==json[attr]){ clearInterval(obj.timer); if(fn){//上一個動作結束后,如果存在fn則調用fn fn(); } }else{ if(attr=='opacity'){ obj.style.filter='alpha(opacity:'+(icur+speed)+')'; obj.style.opacity=(icur+speed)/100; }else{ obj.style[attr]=icur+speed+"px"; } } } },30); } function?getStyle(obj,attr){//通過此函數可無傷取到style,而不受其他樣式的影響 if(obj.currentStyle){//IE瀏覽器 return?obj.currentStyle[attr]; }else{ return?getComputedStyle(obj,false)[attr]; } }
同時運動時,執行onmouseover后寬度并沒有達到400,是因為speed的精度不夠嗎?還是其他的原因?求解答
2016-05-16
if(icur==json[attr]){
????????????????????clearInterval(obj.timer);
????????????????????if(fn){//上一個動作結束后,如果存在fn則調用fn
????????????????????????fn();
????????????????????}
????????????????}
看這段代碼,同時運動時,當某個屬性的當前值達到目標值時,就會關閉定時器,那么其他屬性也就不得不停止運動,所以寬度達不到目標值
2016-05-14
老師后面已經講解了,明白了。。。