js運動基礎之勻速運動

來源:互聯網
上載者:User

標籤:

單個屬性勻速運動
 1     /** 2      * 單屬性勻速運動 3      */ 4     function fnSingleUniformMove(oDom, sAttr, iTarget, fn){ 5         var iSpeed, iCur; 6         clearInterval(oDom.timer); // 開啟定時器前先清除定時器,避免動畫重複疊加 7         oDom.timer = setInterval(function(){ 8  9             // 計算當前值10             if(sAttr == ‘opacity‘){11                 iCur = Math.round(fnGetStyle(oDom, sAttr) * 100);12             }else{13                 iCur = parseInt(fnGetStyle(oDom, sAttr));14             }15 16             // 設定速度17             if(iTarget < iCur){18                 iSpeed = -10;19             }else{20                 iSpeed = 10;21             }22 23             // 當目標點和當前值差值小於速度時速度等於差值24             if(Math.abs(iTarget - iCur) < Math.abs(iSpeed)){25                 iSpeed = iTarget - iCur;26             }27 28             // 運動停止判斷29             if(iTarget == iCur){30                 clearInterval(oDom.timer);31                 fn&&fn();32             }else{33                 if(sAttr == ‘opacity‘){34                     oDom.style.opacity = ( iCur + iSpeed ) / 100;35                     oDom.style.filter = ‘alpha(opacity: ‘ + (iCur + iSpeed) + ‘)‘;36                 }else{37                     oDom.style[sAttr] = iCur + iSpeed + ‘px‘;38                 }39             }40         }, 30);41     };

 

多屬性同時改變的勻速運動

 

 1     /** 2      * 多屬性勻速運動 3      */ 4     function fnMultiUniformMove(oDom, json, fn){ 5         var iSpeed, iCur, bStop; 6         clearInterval(oDom.timer); // 開啟定時器前先清除定時器,避免動畫重複疊加 7         oDom.timer = setInterval(function(){ 8             bStop = true; // 進行for in 迴圈前為true,如果有一個沒達到目標點下面就會讓他變為false,最後判斷該值是否為true來決定是否關閉定時器 9             for(var sAttr in json){10                 // 計算當前值11                 if(sAttr == ‘opacity‘){12                     iCur = Math.round(fnGetStyle(oDom, sAttr) * 100);13                 }else{14                     iCur = parseInt(fnGetStyle(oDom, sAttr));15                 }16 17                 // 設定速度18                 if(json[sAttr] < iCur){19                     iSpeed = -10;20                 }else{21                     iSpeed = 10;22                 }23 24                 // 當目標點和當前值差值小於速度時速度等於差值25                 if(Math.abs(json[sAttr] - iCur) < Math.abs(iSpeed)){26                     iSpeed = json[sAttr] - iCur;27                 }28 29                 // 單屬性運動停止判斷30                 if(json[sAttr] != iCur){31                     bStop = false;32                     if(sAttr == ‘opacity‘){33                         oDom.style.opacity = ( iCur + iSpeed ) / 100;34                         oDom.style.filter = ‘alpha(opacity: ‘ + (iCur + iSpeed) + ‘)‘;35                     }else{36                         oDom.style[sAttr] = iCur + iSpeed + ‘px‘;37                     }38                 }39             }40 41             // 整體運動停止的判斷42             if(bStop){43                 clearInterval(oDom.timer);44                 fn&&fn();45             }46         }, 30);47     };

 

擷取元素樣式的方法
1     // 擷取元素的某個樣式的值2     function fnGetStyle(oDom, sAttr){3         if(getComputedStyle){4             return getComputedStyle(oDom, null)[sAttr]; // getComputedStyle擷取自訂值時返回undefined5         }else{6             return oDom.currentStyle[sAttr]; // currentStyle和getAttribute一樣可以讀取自訂的值(這樣opacity的值才能被擷取)7         };8     };

 

js運動基礎之勻速運動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.