运动
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>试验</title> <style> #div1{width:100px; height:100px; background:red; position:absolute; left:0px;top: 0px;} </style> <script type="text/javascript" src="move.js"></script> <script> window.onload=function(){ var oDiv=document.getElementById('div1'); var timer = null; var taskList = [ toRight, toLeft, toRight, toDown, toLeft, toTop, toDown, toRight, toTop, toLeft, ]; start(taskList); // Math.random(); function start (taskList){ var index = 0; doNext(index); function doNext (){ taskList[index](doNext); index++; if(index>=taskList.length){ index = 0; } } } oDiv.onclick = function() { clearInterval(timer); } function toRight(fn){ timer = setInterval(function (){ if(oDiv.offsetLeft<1100) { oDiv.style.left=oDiv.offsetLeft+40+'px'; } else { clearInterval(timer); fn(); } },30); } function toDown(fn){ timer=setInterval(function (){ if(oDiv.offsetTop<300) { oDiv.style.top=oDiv.offsetTop+40+'px'; } else { clearInterval(timer); fn(); } },30); } function toLeft (fn){ timer=setInterval(function (){ if(oDiv.offsetLeft>0) { oDiv.style.left=oDiv.offsetLeft-40+'px'; } else { clearInterval(timer); fn(); } },30); } function toTop(fn){ timer=setInterval(function (){ if(oDiv.offsetTop>0) { oDiv.style.top=oDiv.offsetTop-40+'px'; } else { clearInterval(timer); fn(); } },30); } } </script> </head> <body> <div id="div1" ></div> </body> </html>
相关推荐
80216130 2020-04-26
83510998 2020-03-26
YUAN 2020-03-06
wanff0 2019-12-30
闪饕 2015-01-20
asnowstar 2019-07-12
yaosir 2019-06-27
Mrlinjw 2014-04-17
scdn 2014-07-10
学习交流可以加0 2016-11-06
过时不侯的温暖 2016-11-02
shottgogoo 2016-09-26
过时不侯的温暖 2016-02-12
badroot 2015-11-26
liwinner 2015-06-27
89701697 2015-05-21
tft0 2018-12-25
云科技时代 2018-12-12
AsiaLee 2018-04-11