1 簡單運動(勻速)
10多年的南崗網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整南崗建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“南崗網(wǎng)站設(shè)計”,“南崗網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
box{ ????width:?100px; ????height:?100px; ????background-color:?#ccc; ????position:?absolute; ????top:200px; ????left:?0; } <script?type="text/javascript"> ????var?obtn?=?document.querySelector('button'); ????var?obox?=?document.querySelector('.box'); ????//?設(shè)置速度 ????var?speed?=?10; ????obtn.onclick=function(){ ????//?1?先清除掉定時器 ????clearInterval(obox.timer); ????????obox.timer?=?setInterval(function(){ ????????obox.style.left?=?obox.offsetLeft?+?speed?+?'px' ????},30); } </script>
2 指定運動的距離(勻速)
js代碼:
<script?type="text/javascript"> ????var?obtn?=?document.querySelector('button'); ????var?obox?=?document.querySelector('.box'); ????var?totalDistance?=?500; ????//?設(shè)置速度 ????var?speed?=?10; ????obtn.onclick?=?function()?{ ????//?1?先清除掉定時器 ????clearInterval(obox.timer); ????obox.timer?=?setInterval(function()?{ ????obox.style.left?=?obox.offsetLeft?+?speed?+?'px' ????if(getStyle(obox,'left')?>=?totalDistance){ ????//?已經(jīng)到達目的地了 ????obox.style.left?=?totalDistance?+?'px'; ????//?同時我們還需要清除掉定時器 ????clearInterval(obox.timer); ????} ????},?30); } //?封裝獲取樣式的方法??不帶px單位的 function?getStyle(ele,?style)?{ ????let?result?=?ele.currentStyle???ele.currentStyle[style]?:?getComputedStyle(ele,?null)[style]; ????return?parseInt(result); } </script>
3 緩沖運動(速度由快到慢,直至停止)
緩沖運動的原理: 速度由距離決定。即: 距離越大速度越大,距離越近,速度越小,直至為0.
4 加速運動(速度由慢到快,直至到達終點)
加速運動和緩沖運動相反,代碼也不需要做過多的修改
原理:根據(jù)移動的距離來設(shè)置速度,也就是正比關(guān)系
var?obtn?=?document.querySelector('button'); var?obox?=?document.querySelector('.box'); var?totalDistance?=?500; //?設(shè)置速度 var?speed?=?null; obtn.onclick?=?function()?{ //?1?先清除掉定時器 clearInterval(obox.timer); obox.timer?=?setInterval(function()?{ //?1?獲取當前運動的距離 var?curPosition?=?getStyle(obox,'left'); //?2?speed是變化的?動態(tài)計算 speed?=?(curPosition?/?10)||1; //?對speed進行取整操作 //?ceil:向上取整 //?floor:?向下取整 //?3?*需要對speed進行取整?否則達不到臨界值 speed?=?speed?>?0??Math.ceil(speed):Math.floor(speed); obox.style.left?=?obox.offsetLeft?+?speed?+?'px'; console.log(speed); if(getStyle(obox,'left')?>=?totalDistance){ console.log('我執(zhí)行了沒'); obox.style.left?=?totalDistance?+?'px'; clearInterval(obox.timer); } },?30); } //?封裝獲取樣式的方法??不帶px單位的 function?getStyle(ele,?style)?{ ????let?result?=?ele.currentStyle???ele.currentStyle[style]?:?getComputedStyle(ele,?null)[style]; ????return?parseInt(result); }
當前題目:js中的運動
文章出自:http://chinadenli.net/article34/joeise.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、微信公眾號、動態(tài)網(wǎng)站、外貿(mào)建站、軟件開發(fā)、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)