<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> *{ margin:0; padding:0; } #box{ position:absolute; top:0; left:0; padding:0; width:100px; height:100px; background:lightblue; }</style> </head> <body> <div id='box'></div> <script>//思想1:var oBox = document.getElementById('box');//實現一個勻速運動:在指定時間內完成動畫:計算出總距離/總時間,然后求出對應的步長(每10ms走一步的話需要走多遠)var maxLeft = (document.documentElement.clientWidth || document.body.clientWidth) - oBox.offsetWidth;var duration = 2000;var step = (maxLeft/duration)*10;var timer = window.setInterval(function(){var curLeft = utils.css(oBox,"left"); curLeft+=step;if(curLeft>=maxLeft){ window.clearInterval(timer);return; } utils.css(oBox,"left",curLeft); },10)//思想2:function Linear(t,b,c,d){//相對應的是time begin change durationreturn c*t/d+b }var oBox = document.getElementById('box');var target = (document.documentElement.clientWidth || document.body.clientWidth) - oBox.offsetWidth;var begin = utils.css(oBox,'left'),change = target-begin;var duration = 2000,time = null;var timer = window.setInterval(function(){ time+=10;if(time>=duration){ utils.css(oBox,"left",target); window.clearInterval(timer);return; }var curPos = Linear(time,begin,change,duration); utils.css(oBox,"left",curPos) },10)</script> </body> </html>
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com