请帮忙看下这个,怎么停不下来?

来源:2-1 JS速度动画

慕粉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>


写回答 关注

2回答

  • Zzmkiv
    2017-08-19 11:23:31
    已采纳

    代码是没错的,但是你没有清除浏览器默认样式。*{margin:0;padding:0},所以你这里每次加上speed  10px,得不到left值为0,会直接由-2px跳到16px

    慕粉3243...

    非常感谢!

    2017-09-06 07:56:51

    共 1 条回复 >

  • 为彭十七加油oO
    2017-08-06 14:53:05

    我也看不出来哪里出错了。楼主现在发现哪里错了吗? 指点一下-.-!

    慕粉3243...

    我还是改成最后的代码才好的,这段代码不知道怎么和老师的效果不同

    2017-08-06 22:59:36

    共 1 条回复 >

JS动画效果

通过本课程JS动画的学习,从简单动画开始,逐步深入各种动画框架封装

113931 学习 · 1443 问题

查看课程

相似问题