JavaScript 運動框架 Step by step(轉)


1,運動原理

Js運動,本質來說,就是讓 web 上 DOM 元素動起來。而想要 DOM 動起來,改變其自身的位置屬性,比如高寬,左邊距,上邊距,透明度等。動畫的原理就是把不同狀態的物體,串成連續的樣子,就像一本書,畫了幾個小人,然后一翻書,就看見小人在動。js動畫也一樣。不同狀態的DOM,用定時器控制,就能得到動畫效果。

[javascript] view plain copy
  1. window.onload = function(){  
  2.     var oBtn = document.getElementById('btn');  
  3.     oBtn.onclick = function(){  
  4.         var oDiv = document.getElementById('div1');  
  5.         //設置定時器  
  6.         setInterval(function(){  
  7.             //改變物體位置  
  8.             oDiv.style.left = oDiv.offsetLeft + 10 + 'px';  
  9.         },30)  
  10.                                                                                                                                                                                                                                                                       
  11.     }  
  12. }  


上述代碼,點擊btn之后,就能是物體向左運動。可是會一直向右動,不會停止。因此需要創立一個停止的條件。在條件符合的情況下,清楚定時器。其中對於目標點的判斷,尤為重要。

[javascript] view plain copy
  1. window.onload = function(){  
  2.     var oBtn = document.getElementById('btn');  
  3.     oBtn.onclick = function(){  
  4.         var oDiv = document.getElementById('div1');  
  5.         //設置定時器  
  6.         var timer = setInterval(function(){  
  7.             //判斷停止條件  
  8.             if(oDiv.offsetLeft > 300){  
  9.                 clearInterval(timer);  
  10.             }else{  
  11.                 //改變物體位置  
  12.                 oDiv.style.left = oDiv.offsetLeft + 10 + 'px';  
  13.                 document.title = oDiv.offsetLeft;  
  14.             }  
  15.         },30);  
  16.                                                                                                                                                                                                                                                            
  17.     }  
  18. }  


上述代碼中,但物體的位置大於300的時候,將停止運動。但是上述代碼還有個問題,就是連續點擊按鈕,物體會運動越來越快。因為每點擊一次,就開了一個定時器,累加的定時器。造成運動混亂。

2,運動框架 (滑入出,淡入淡出)

為了解決上述問題,則必須在開啟定時器之前,先清除定時器,因此需要一個全局變量 timer保存定時器。如下面代碼。

[javascript] view plain copy
  1. window.onload = function(){  
  2.     var oBtn = document.getElementById('btn');  
  3.     oBtn.onclick = function(){  
  4.         startMove();  
  5.     }  
  6. }  
  7. var timer = null;  
  8. function startMove(){  
  9.     var oDiv = document.getElementById('div1');  
  10.     clearInterval(timer);  
  11.     //設置定時器  
  12.     timer = setInterval(function(){  
  13.         //判斷停止條件  
  14.         if(oDiv.offsetLeft > 300){  
  15.             clearInterval(timer);  
  16.         }else{  
  17.             //改變物體位置  
  18.             oDiv.style.left = oDiv.offsetLeft + 10 + 'px';  
  19.             document.title = oDiv.offsetLeft;  
  20.         }  
  21.     },30);  
  22. }  


此外,在改變物體位置的時候,那個 “10”則是更改的數量,其實也就是速度。如果更改速度,運動的快慢就能確定。因此,運動框架的原理,基本步驟為。

  • 先清除定時器

  • 開啟定時器,計算速度

  • 判斷停止條件,執行運動

[javascript] view plain copy
  1. var timer = null;  
  2. function startMove(){  
  3.     var oDiv = document.getElementById('div1');  
  4.     clearInterval(timer);  
  5.         //計算速度  
  6.     var iSpeed = 10;  
  7.     //設置定時器  
  8.     timer = setInterval(function(){  
  9.         //判斷停止條件  
  10.         if(oDiv.offsetLeft > 300){  
  11.             clearInterval(timer);  
  12.         }else{  
  13.             //改變物體位置  
  14.             oDiv.style.left = oDiv.offsetLeft + iSpeed + 'px';  
  15.             document.title = oDiv.offsetLeft;  
  16.         }  
  17.     },30);  
  18. }  


對於停止條件,寫死在里面了,所以需分離出參數。下面是一個分享到的例子。主要是根據目標判斷速度的正負。從而在鼠標滑入畫出時候進行運動/恢復的效果。

[javascript] view plain copy
  1. window.onload = function(){  
  2.     var oDiv = document.getElementById('div1');  
  3.     oDiv.onmouseover = function(){  
  4.         startMove(0);  
  5.     }  
  6.     oDiv.onmouseout = function(){  
  7.         startMove(-100);  
  8.     }  
  9. }  
  10. var timer = null;  
  11. var iSpeed;  
  12. function startMove(iTatget){  
  13.     var oDiv = document.getElementById('div1');  
  14.     clearInterval(timer);  
  15.     timer = setInterval(function(){  
  16.         //計算速度  
  17.         if(iTatget -oDiv.offsetLeft > 0){  
  18.             iSpeed = 10;  
  19.         }else{  
  20.             iSpeed = -10;  
  21.         }  
  22.                                                                                                                                                                                                                                
  23.         if(oDiv.offsetLeft == iTatget){  
  24.             clearInterval(timer);  
  25.         }else{  
  26.             oDiv.style.left = oDiv.offsetLeft + iSpeed + 'px';  
  27.         }  
  28.         document.title = oDiv.offsetLeft;  
  29.     },30)  
  30. }  


另外一個小例子,淡入淡出,即改變物體的透明度,由於沒有像原生的位置屬性那樣的offsetLset. 需要一個變量來保存透明度的值,用來和速度加減,最后付給元素的透明度樣式。從而實現淡入淡出效果。

[javascript] view plain copy
  1. window.onload = function(){  
  2.     var oImg = document.getElementById('img1');  
  3.     oImg.onmouseover = function(){  
  4.         startMove(100);  
  5.     }  
  6.     oImg.onmouseout = function(){  
  7.         startMove(30);  
  8.     }  
  9. }  
  10. var timer = null;  
  11. //保存透明度的數字值  
  12. var alpha = 30;  
  13. function startMove(iTarget){  
  14.     var oDiv = document.getElementById('img1');  
  15.     clearInterval(timer);  
  16.     timer = setInterval(function(){  
  17.         var iSpeed = 0;  
  18.         if(alpha > iTarget){  
  19.             iSpeed = -1;  
  20.         }else{  
  21.             iSpeed = 1;  
  22.         }  
  23.         if(alpha == iTarget){  
  24.             clearInterval(timer);  
  25.         }else{  
  26.                         //改變透明度速度值  
  27.             alpha += iSpeed;  
  28.             oDiv.style.filter = 'alpha(opacity:'+ alpha+')';  
  29.             oDiv.style.opacity = alpha/100;  
  30.             document.title = alpha;  
  31.         }  
  32.                                                                                                                                                                                                          
  33.     },30)  
  34. }  


3,緩沖運動

緩沖運動原理就是,改變速度的值。每次累加的速度值變小,就是會是整個物體看起來越來越慢,以至於最后停掉。相當於改變使物體具有一個加速度。這個加速度,可以由物體當前位置和目標位置之間的距離得到,因為兩者之間的距離一直在變小,所以速度也一直在變小。如下:

[javascript] view plain copy
  1. window.onload = function(){  
  2.     var btn = document.getElementsByTagName('input')[0];  
  3.     btn.onclick = function(){  
  4.         startMove(300);  
  5.     }  
  6. }  
  7. var timer = null;  
  8. function startMove(iTarget){  
  9.     var oDiv = document.getElementById('div1');  
  10.     clearInterval(timer);  
  11.     timer = setInterval(function(){  
  12.         //求出帶有變化的速度   
  13.         var iSpeed = (iTarget - oDiv.offsetLeft) / 8;  
  14.         if(oDiv.offsetLeft == iTarget){  
  15.             clearInterval(timer);  
  16.         }else{  
  17.             oDiv.style.left = oDiv.offsetLeft + iSpeed + 'px';  
  18.         }  
  19.         document.title = oDiv.offsetLeft + '...' + iSpeed;  
  20.     },30);  
  21. }  

上述方法可以得到緩沖運動,但是實際運行效果,物體並沒有在 300的位置停掉,而是在 293的位置就停掉了。究其原因。因為當物體的速度小於1 的時候。物體位置為293。此時計算的速度是 0.875.通過 oDiv.style.left = oDiv.offsetLeft + iSpeed + 'px'; 物體的位置為 293.875.可是計算機不能識別小數,將小數省略了。此時的 位置offsetLeft仍然是 293.再計算一次,還是同樣的結果。定時器沒有關掉,但是物體的位置卻再也無法改變,故停留在了 293的位置。解決方案,就是將速度進行向上取整。但是,像上述運動,速度是正的,可是,當速度是負的時候,就同樣會有相同的結果,因此需要在速度為負的時候,向下取整。

[javascript] view plain copy
  1. function startMove(iTarget){  
  2.     var oDiv = document.getElementById('div1');  
  3.     clearInterval(timer);  
  4.     timer = setInterval(function(){  
  5.         var iSpeed = (iTarget - oDiv.offsetLeft) / 8;  
  6.         //對正的速度向上取整,負的速度向下取整  
  7.         iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);  
  8.         if(oDiv.offsetLeft == iTarget){  
  9.             clearInterval(timer);  
  10.         }else{  
  11.             oDiv.style.left = oDiv.offsetLeft + iSpeed + 'px';  
  12.         }  
  13.         document.title = oDiv.offsetLeft + '...' + iSpeed;  
  14.     },30);  
  15. }  


4.多物體運動

下一步,就是處理多物體運動,運動函數里面每次都要選取一個元素加事件。如果需要對多個物體進行同樣的運動, 需要將運動對象作為參數傳進來。

[javascript] view plain copy
  1. window.onload = function(){  
  2.     var aDiv = document.getElementsByTagName('div');  
  3.     for(var i=0;i<aDiv.length;i++){  
  4.         aDiv[i].onmouseover = function(){  
  5.             startMove(this,300);  
  6.         }  
  7.         aDiv[i].onmouseout = function(){  
  8.             startMove(this,100);  
  9.         }  
  10.     }  
  11. }  
  12. var timer = null;  
  13. function startMove(obj,iTarget){  
  14.     clearInterval(timer);  
  15.     timer = setInterval(function(){  
  16.         var iSpeed = (iTarget - obj.offsetWidth) / 8;  
  17.         iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);  
  18.         if(obj.offsetWidth == iTarget){  
  19.             clearInterval(timer);  
  20.         }else{  
  21.             obj.style.width = obj.offsetWidth + iSpeed + 'px';  
  22.         }  
  23.                                                                                                                                                          
  24.     },30)  
  25. }  


通過循環物體,將物體的 this傳給運動函數,使得多物體可以運動。但是這樣有一個弊端,即當滑入第一個運動的時候,開啟了定時器。如果此時,滑入另外一個物體,將會清理上一個定時器。這就造成了,上一次運動,很有可能還沒完成結束,定時器就沒關閉了。解決的方法,每個運動的物體,都能開了一個屬於自己的定時器。因此,把定時器當成物體的屬性。清理的時候也就是清理自己的定時器。

[javascript] view plain copy
  1. window.onload = function(){  
  2.     var aDiv = document.getElementsByTagName('div');  
  3.     for(var i=0;i<aDiv.length;i++){  
  4.         aDiv[i].onmouseover = function(){  
  5.             startMove(this,300);  
  6.         }  
  7.         aDiv[i].onmouseout = function(){  
  8.             startMove(this,100);  
  9.         }  
  10.     }  
  11. }  
  12. function startMove(obj,iTarget){  
  13.     //將定時器,變成物體的屬性,類似給物體添加索引  
  14.     clearInterval(obj.timer);  
  15.     obj.timer = setInterval(function(){  
  16.         var iSpeed = (iTarget - obj.offsetWidth) / 8;  
  17.         iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);  
  18.         if(obj.offsetWidth == iTarget){  
  19.             clearInterval(obj.timer);  
  20.         }else{  
  21.             obj.style.width = obj.offsetWidth + iSpeed + 'px';  
  22.         }  
  23.                                                                                                                                              
  24.     },30)  
  25. }  


多物體的淡入淡出的時候,也有類似的問題。因為修改透明度的時候,是先用一個變量保存透明度,必須針對每個物體設立透明度值屬性。

[javascript] view plain copy
  1. window.onload = function(){  
  2.     var aDiv = document.getElementsByTagName('div');  
  3.     for(var i=0;i<aDiv.length;i++){  
  4.         //將透明度值當初屬性  
  5.         aDiv[i].alpha = 30;  
  6.         aDiv[i].onmouseover = function(){  
  7.             startMove(this,100);  
  8.         }  
  9.         aDiv[i].onmouseout = function(){  
  10.             startMove(this,30);  
  11.         }  
  12.     }  
  13. }  
  14. function startMove(obj,iTarget){      
  15.     clearInterval(obj.timer);  
  16.     obj.timer = setInterval(function(){  
  17.         var iSpeed = (iTarget - obj.alpha) / 8;  
  18.         iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);  
  19.         if(obj.alpha == iTarget){  
  20.             clearInterval(obj.timer);  
  21.         }else{  
  22.             obj.alpha += iSpeed;  
  23.             obj.style.filter = 'alpha(opacity:'+obj.alpha+')';  
  24.             obj.style.opacity = obj.alpha / 100;  
  25.         }  
  26.         document.title = obj.alpha;  
  27.     },30);  
  28. }  


4.1  位置屬性的bug

offsetWidth 或者 offsetHeight 等位置屬性,一旦給他們加上 border。則會有詭異的現象出現。

[javascript] view plain copy
  1. window.onload = function(){  
  2.         var oDiv = document.getElementById('div1');  
  3.         setInterval(function(){  
  4.             oDiv.style.width = oDiv.offsetWidth - 1 + "px";  
  5.         },30)  
  6.     }  


例如 oDiv.style.width = oDiv.offsetWidth - 1 + 'px'; 如果給 oDiv 的width 為一百,border 為 1.則這個物體的 width是100px;offsetWidth 為102px;帶入公式之后,即減一之后。100 = 102 - 1 ,反而等於101;即 物體本來要減小,事實卻增大了。解決的方案就是,加減的時候,必須使用物體的內聯樣式。但是 火狐 和 IE 又有兼容模式。解決方案如下:

[javascript] view plain copy
  1. window.onload = function(){  
  2.         var oDiv = document.getElementById('div1');  
  3.         setInterval(function(){  
  4.                                                                                                                    
  5.             oDiv.style.width = parseInt(getStyle(oDiv,'width')) - 1 + 'px';  
  6.                                                                                                         
  7.         },30)  
  8.     }  
  9.     function getStyle(obj,attr){  
  10.         if(obj.currentStyle){  
  11.             return obj.currentStyle[attr];  
  12.         }else{  
  13.             return getComputedStyle(obj,false)[attr];  
  14.         }  
  15.     }  


其中,getStyle函數,傳入一個元素對象,和其 css 屬性,獲取的是元素的樣式,即 witdh 100px;因此需要parseInt轉換

5.任意值運動

通過 getStyle 函數,可以獲取元素的樣式,還可也通過 attr 制定需要修改的 css屬性。這樣就能是物體有不同的運動形式。

[javascript] view plain copy
  1. window.onload = function(){  
  2.     var aDiv = document.getElementsByTagName('div');  
  3.     aDiv[0].onmouseover = function(){  
  4.         startMove(this,'width',300);  
  5.     }  
  6.     aDiv[0].onmouseout = function(){  
  7.         startMove(this,'width',100);  
  8.     }  
  9.     aDiv[1].onmouseover = function(){  
  10.         startMove(this,'height',100);  
  11.     }  
  12.     aDiv[1].onmouseout = function(){  
  13.         startMove(this,'height',50);  
  14.     }  
  15. }  
  16. function getStyle(obj,attr){  
  17.     if(obj.currentStyle){  
  18.         return obj.currentStyle(attr);  
  19.     }else{  
  20.         return getComputedStyle(obj,false)[attr];  
  21.     }  
  22. }  
  23. function startMove(obj,attr,iTarget){  
  24.     clearInterval(obj.timer);  
  25.     obj.timer = setInterval(function(){  
  26.         var iCur = parseInt(getStyle(obj,attr));  
  27.         var iSpeed = (iTarget - iCur) / 8;  
  28.         iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);  
  29.         if(iCur == iTarget){  
  30.             clearInterval(obj.timer);  
  31.         }else{  
  32.             obj.style[attr] = iCur + iSpeed + 'px';  
  33.         }  
  34.                                                                                                     
  35.     },30)  
  36. }  


5.1 任意值完美版

上述版本,還不能處理透明度的任意值,因此需要增加額外的兼容hack。

[javascript] view plain copy
  1. window.onload = function(){  
  2.         var aDiv = document.getElementsByTagName('div');  
  3.         aDiv[0].onmouseover = function(){  
  4.             startMove(this,'opacity',100);  
  5.         }  
  6.         aDiv[0].onmouseout = function(){  
  7.             startMove(this,'opacity',30);  
  8.         }  
  9.     }  
  10.     function getStyle(obj,attr){  
  11.         if(obj.currentStyle){  
  12.             return obj.currentStyleattr[attr];  
  13.         }else{  
  14.             return getComputedStyle(obj, false)[attr];  
  15.         }  
  16.     }  
  17.     function getStyle(obj, attr){  
  18.         if(obj.currentStyle)    {  
  19.             return obj.currentStyle[attr];  
  20.         }else{  
  21.             return getComputedStyle(obj, false)[attr];  
  22.         }  
  23.     }  
  24.     function startMove(obj,attr,iTarget){  
  25.         clearInterval(obj.timer);  
  26.         obj.timer = setInterval(function(){  
  27.             var iCur = 0;  
  28.             if(attr == 'opacity'){  
  29.                 iCur = parseInt(parseFloat(getStyle(obj, attr))*100);  
  30.             }else{  
  31.                 iCur = parseInt(getStyle(obj,attr));  
  32.             }  
  33.             var iSpeed = (iTarget - iCur) / 8;  
  34.             iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);  
  35.             if(iCur == iTarget){  
  36.                 clearInterval(obj.timer);  
  37.             }else{  
  38.                 if(attr=='opacity'){  
  39.                     iCur += iSpeed  
  40.                     obj.style.filter='alpha(opacity:' + iCur + ')';  
  41.                     obj.style.opacity=iCur / 100;  
  42.                 }  
  43.                 else{  
  44.                     obj.style[attr]=iCur+iSpeed+'px';  
  45.                 }  
  46.                 document.title = obj.style[attr];  
  47.             }  
  48.                                                                                       
  49.         },30)  
  50.     }  


6.鏈式運動

我們的運動框架到目前為止,基本功能都能實現了。現在拓展。所謂鏈式運動,即運動接着運動。當運動停止的時候,如果回調一個函數。回調一個運動函數,就能出現這樣的效果。因此傳入一個函數作為回調函數。

[javascript] view plain copy
  1. window.onload = function(){  
  2.     var oDiv = document.getElementById('div1');  
  3.     oDiv.onclick = function(){  
  4.         startMove(this,'width',300,function(){  
  5.             startMove(oDiv,'height',300,function(){  
  6.                 startMove(oDiv,'opacity',100)  
  7.             })  
  8.         })  
  9.     }  
  10. }  
  11. function getStyle(obj,attr){  
  12.     if(obj.currentStyle){  
  13.         return obj.currentStyleattr[attr];  
  14.     }else{  
  15.         return getComputedStyle(obj, false)[attr];  
  16.     }  
  17. }  
  18.                                                                       
  19. function startMove(obj,attr,iTarget,fn){  
  20.     clearInterval(obj.timer);  
  21.     obj.timer = setInterval(function(){  
  22.         var iCur = 0;  
  23.         if(attr == 'opacity'){  
  24.             iCur = parseInt(parseFloat(getStyle(obj, attr))*100);  
  25.         }else{  
  26.             iCur = parseInt(getStyle(obj,attr));  
  27.         }  
  28.         var iSpeed = (iTarget - iCur) / 8;  
  29.         iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);  
  30.         if(iCur == iTarget){  
  31.             clearInterval(obj.timer);  
  32.             //回調函數  
  33.             if(fn) fn();  
  34.         }else{  
  35.             if(attr=='opacity'){  
  36.                 iCur += iSpeed  
  37.                 obj.style.filter='alpha(opacity:' + iCur + ')';  
  38.                 obj.style.opacity=iCur / 100;  
  39.             }  
  40.             else{  
  41.                 obj.style[attr]=iCur+iSpeed+'px';  
  42.             }  
  43.             document.title = obj.style[attr];  
  44.         }  
  45.                                                                         
  46.     },30)  
  47. }  


7.同時運動

目前為止,我們的運動框架還有個小缺點,就是不能同時該兩個屬性進行運動,比如同時更改寬和高。這個要求傳入的屬性是不同的幾個值。則考慮傳入一個 json用來保存需要更改的屬性。

[javascript] view plain copy
  1. window.onload = function(){  
  2.     var oDiv = document.getElementById('div1');  
  3.     oDiv.onclick = function(){  
  4.         startMove(this,{'width':300,'height':400});  
  5.     }  
  6. }  
  7. function getStyle(obj, attr){  
  8.     if(obj.currentStyle)    {  
  9.         return obj.currentStyle[attr];  
  10.     }else{  
  11.         return getComputedStyle(obj, false)[attr];  
  12.     }  
  13. }  
  14. function startMove(obj,json,fn){  
  15.     clearInterval(obj.timer);  
  16.     obj.timer = setInterval(function(){  
  17.      // 循環json   
  18.      for(var attr in json){        
  19.             var iCur = 0;  
  20.             if(attr == 'opacity'){  
  21.                 iCur = parseInt(parseFloat(getStyle(obj, attr))*100);  
  22.             }else{  
  23.                 iCur = parseInt(getStyle(obj,attr));  
  24.             }  
  25.             var iSpeed = (json[attr] - iCur) / 8;  
  26.             iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);  
  27.             if(iCur == json[attr]){  
  28.                 clearInterval(obj.timer);  
  29.                 if(fn) fn();  
  30.             }else{  
  31.                 if(attr=='opacity'){  
  32.                     iCur += iSpeed  
  33.                     obj.style.filter='alpha(opacity:' + iCur + ')';  
  34.                     obj.style.opacity=iCur / 100;  
  35.                 }  
  36.                 else{  
  37.                     obj.style[attr]=iCur+iSpeed+'px';  
  38.                 }  
  39.                 document.title = obj.style[attr];  
  40.             }  
  41.         }  
  42.     },30)  


上述代碼,可以解決了同時運動的問題。但是還是有一個bug。比如,同時運動的某個屬性,如果變化很小,馬上就停止了,即關掉了定時器。那么會造成其他屬性的變化也停止。因為這些屬性都共用了一個定時器。因此需要判斷,假設有三個人要來,然后一起去爬山。三個人有的先來,有的后來,只要三個人都到齊了,才出發。也就是只有三個屬性都到了目標值,才關定時器。一開始,設立一個檢查量,為真。假設所有人都到了,然后循環,只有有一個人沒有到,檢查就為假。直到所有的都到了,檢測為真。則停止定時器。

[javascript] view plain copy
  1. window.onload = function(){  
  2.     var oDiv = document.getElementById('div1');  
  3.     oDiv.onclick = function(){  
  4.         startMove(this,{'width':102,'height':400,'opacity':100});  
  5.     }  
  6. }  
  7. function getStyle(obj, attr){  
  8.     if(obj.currentStyle)    {  
  9.         return obj.currentStyle[attr];  
  10.     }else{  
  11.         return getComputedStyle(obj, false)[attr];  
  12.     }  
  13. }  
  14. function startMove(obj,json,fn){  
  15.     clearInterval(obj.timer);  
  16.     obj.timer = setInterval(function(){  
  17.         var bStop = true;  
  18.         for(var attr in json){    
  19.             //取當前值    
  20.             var iCur = 0;  
  21.             if(attr == 'opacity'){  
  22.                 iCur = parseInt(parseFloat(getStyle(obj, attr))*100);  
  23.             }else{  
  24.                 iCur = parseInt(getStyle(obj,attr));  
  25.             }  
  26.             //計算速度  
  27.             var iSpeed = (json[attr] - iCur) / 8;  
  28.             iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);  
  29.             //檢測停止  
  30.             if(iCur != json[attr]){  
  31.                 bStop = false;  
  32.             }     
  33.             if(attr=='opacity'){  
  34.                 iCur += iSpeed  
  35.                 obj.style.filter='alpha(opacity:' + iCur + ')';  
  36.                 obj.style.opacity=iCur / 100;  
  37.             }  
  38.             else{  
  39.                 obj.style[attr]=iCur+iSpeed+'px';  
  40.             }  
  41.         }  
  42.         if(bStop){  
  43.             clearInterval(obj.timer);  
  44.             if(fn) fn();  
  45.         }  
  46.     },30)  
  47. }  


再循環外定義一個 標志變量 bStop = true。用來表示所有屬性到達目標值。等循環結束了,如果這個值是真的,則停止定時器。因為,每次運行定時器,都會初始化這個值。循環的過程中,只要有一個沒有到,bStop就被設定為 false。如果某個到了,此時 iCur != json[attr],表示速度為0 后面執行的結果,也不會有變化。只有所有的都達到目標值。循環則不再改變 bStop的值。此時,只要下一次運行定時器。就是初始化 bStop為真。而循環因為都到了,所以速度為0 也就再也沒有變化。循環結束,sBstop還是真,表示所有都到了。因此此時結束定時器。

 

最后附上完美運動框架,封裝成 move.js 就可以調用了。

[javascript] view plain copy
  1. /** 
  2.  * @author rsj217 
  3.  * getStyle 獲取樣式 
  4.  * startMove 運動主程序 
  5.  */  
  6.                  
  7. function getStyle(obj, attr){  
  8.     if(obj.currentStyle)    {  
  9.         return obj.currentStyle[attr];  
  10.     }else{  
  11.         return getComputedStyle(obj, false)[attr];  
  12.     }  
  13. }  
  14. function Move(obj,json,fn){  
  15.     //停止上一次定時器  
  16.     clearInterval(obj.timer);  
  17.     //保存每一個物體運動的定時器  
  18.     obj.timer = setInterval(function(){  
  19.         //判斷同時運動標志  
  20.         var bStop = true;  
  21.         for(var attr in json){    
  22.             //取當前值    
  23.             var iCur = 0;  
  24.             if(attr == 'opacity'){  
  25.                 iCur = parseInt(parseFloat(getStyle(obj, attr))*100);  
  26.             }else{  
  27.                 iCur = parseInt(getStyle(obj,attr));  
  28.             }  
  29.             //計算速度  
  30.             var iSpeed = (json[attr] - iCur) / 8;  
  31.             iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);  
  32.             //檢測同時到達標志  
  33.             if(iCur != json[attr]){  
  34.                 bStop = false;  
  35.             }     
  36.             //更改屬性,獲取動畫效果  
  37.             if(attr=='opacity'){  
  38.                 iCur += iSpeed  
  39.                 obj.style.filter='alpha(opacity:' + iCur + ')';  
  40.                 obj.style.opacity=iCur / 100;  
  41.             }  
  42.             else{  
  43.                 obj.style[attr]=iCur+iSpeed+'px';  
  44.             }  
  45.         }  
  46.         //檢測停止  
  47.         if(bStop){  
  48.             clearInterval(obj.timer);  
  49.             if(fn) fn();  
  50.         }  
  51.     },30)  
  52. }  

轉自:http://blog.csdn.net/rsj217/article/details/7986905


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM