本文實(shí)例為大家分享了setTimeout實(shí)現(xiàn)輪循動(dòng)畫的具體代碼,供大家參考,具體內(nèi)容如下
代碼如下:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title></head><body> <div id='box'></div> <script> var oBox = document.getElementById("box"); var maxLeft = utils.win('clientWidth')-oBox.offsetWidth; var step = 5; var timer = null; //使用遞歸思想完成setTimeout的輪循動(dòng)畫:每一次在執(zhí)行動(dòng)畫之前把上一次設(shè)置沒用的定時(shí)器清除掉,節(jié)約我們的內(nèi)存空間 function move(){ window.clearTimeout(timer); var curLeft = utils.css(oBox,"left"); if(curLeft+step >= maxLeft){//邊界判斷 utils.css(oBox,"left",maxLeft); return; } curLeft+=step; utils.css(oBox,"left",curLeft); timer = window.setTimeout(move,10) } move(); </script></body></html>以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
新聞熱點(diǎn)
疑難解答