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

JS缓冲动画中为什么会卡住闪动?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

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

<title>无标题文档</title>

<style>


*{margin:0; padding:0}

.bigBox

{

width:200px; 

height:200px; 

left: -200px;

top:120px; 

background:red;

position:relative;

opacity:30;

}

span

{

width: 20px;

height: 50px;

background: blue;

position: absolute;

left: 200px;

top: 75px;

}


</style>

<script type="text/javascript" >


window.onload=function()

{

var movebigBox=document.getElementById("big");

var timerId = null;

movebigBox.onmouseover=function(){moveFun(0)};

movebigBox.onmouseout=function(){moveFun(-200)};

function moveFun(target)

{

clearInterval(timerId);

   var timerId=setInterval(function move()

{  

  var speed=(target-movebigBox.offsetLeft)/20;

  speed=speed>0?Math.ceil(speed):Math.floor(speed);

  if(movebigBox.offsetLeft==target)

{

clearInterval(timerId);

}

else

//document.title=speed;

movebigBox.style.left  = movebigBox.offsetLeft+speed+"px";

}

},30);

}

}


</script>

</head>

<body>

  <div class="bigBox" id="big" >

  <span>分享</span>

  </div>

</body>

</html>


正在回答

2 回答

你的这句话写错了:

 var timerId=setInterval(function move()

你先前已经定义过了,但是你这里又重新定义了一遍,但是javascript的运行是从上到下的,他认为你重新定义了一个新的timerId,简单说你去掉这个var就正常了,改成这样:

timerId=setInterval(function move()


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

慕粉studying 提问者

非常感谢!
2016-11-07 回复 有任何疑惑可以回复我~

说实话  我都听不懂你的问题  卡住闪动?

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

举报

0/150
提交
取消
JS动画效果
  • 参与学习       113936    人
  • 解答问题       1443    个

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

进入课程

JS缓冲动画中为什么会卡住闪动?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信