为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我的offsetLeft每次加9px

<!doctype>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

<title>透明度动画</title>

<style type="text/css">

#wrap{width:230px;height:105px;position: relative;left:-217px;}

#red{width:200px;height:100px;background-color: red;position: absolute;}

#blue{width:30px;height:30px;background-color: blue;margin-left: 200px;position: absolute;}

</style>

<script type="text/javascript">

window.onload=function(){

var wrap=document.getElementById("wrap");

var timer=null

wrap.onmouseover=function(){

startMove();

}

}


function startMove(){

timer=setInterval(function(){ 


if(wrap.offsetLeft==0){

clearInterval(timer);

}

else{


wrap.style.left=wrap.offsetLeft+1+'px'; //console.log(wrap.offsetLeft);

}

},300);

}

</script>

</head>

<body>

<div id="wrap">

<div id="red"></div>

<div id="blue"></div>                                                        

</div>

</body>

</html>

为什么我的offsetLeft每次加9px

正在回答

1 回答

因为wrap获取的offsetLeft不是217  而是209

他把body的margin也算进去了

修改办法:

在样式里添加:  *{ padding:0; margin:0}

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

为什么我的offsetLeft每次加9px

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号