<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1 {width:100px; height:100px; background:red; position:absolute; left:600px; top:50px;}
#div2 {width:1px; height:300px; background:black; position:absolute; left:300px; top:0}
</style>
<script>
function startMove()
{
var oDiv=document.getElementById('div1');
setInterval(function() {
var speed=(300-oDiv.offsetLeft)/10;
//speed=Math.ceil(speed); speed=speed>0?Math.ceil(speed):Math.floor(speed);
//在做运动的时候一定不要忘了取整,浏览器会把小数舍弃ceil向上取整floor向下取整
oDiv.style.left=oDiv.offsetLeft+speed+'px'; document.title=oDiv.offsetLeft ;
}, 30);
}
</script>
</head> <body>
<input type="button" value="开始运动" onClick="startMove()"/>
<div id="div1">
</div>
<div id="div2"></div>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)