慕粉3243312
2017-08-06 13:10
<!DOCTYPE html>
<html>
<head>
<title>动画</title>
<meta charset="utf-8">
<style type="text/css">
#div{
width: 200px;
height: 200px;
background: red;
position: relative;
left: -200px;
top: 0;
}
#div span{
width: 20px;
height: 50px;
background: blue;
position: absolute;
left: 200px;
top: 75px;
}
</style>
<script type="text/javascript">
window.onload=function()
{
var oDiv=document.getElementById('div');
oDiv.onmouseover=function()
{
startmove();
}
}
var timer =null;
function startmove()
{
clearInterval(timer);
var oDiv=document.getElementById('div');
var speed=10;
timer=setInterval(function(){
if (oDiv.offsetLeft == 0)
{
clearInterval(timer);
}else{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
},30)
}
</script>
</head>
<body>
<div id="div" ><span id="share">分享</span></div>
</body>
</html>
代码是没错的,但是你没有清除浏览器默认样式。*{margin:0;padding:0},所以你这里每次加上speed 10px,得不到left值为0,会直接由-2px跳到16px
我也看不出来哪里出错了。楼主现在发现哪里错了吗? 指点一下-.-!
JS动画效果
113910 学习 · 1502 问题
相似问题