<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
div{
width: 200px;
background-color: red;
height:100px;
position: relative;
}
</style>
<script type="text/javascript">
window.onload=function (){
var div=document.getElementById("DIV");
var btn=document.getElementById("right");
var timer=null;
btn.onclick=function(){
clearInterval(timer);
timer=setInterval(function(){
if(div.offsetLeft==400){
clearInterval(timer);
}
else{div.style.left=div.offsetLeft+1+'px';}
},50)
};
};
</script>
</head>
<body>
<div id="DIV"></div>
<input type="button" value="right" id="right">
</body>
</html>
如果改了if(div.offsetLeft>=400) 则会在left:396px 停下来,这又是为什么?
Walk_
相关分类