javascript - 缓冲运动中为什么第二次运动的时候会停在中间位置左右晃动?
天蓬老师
天蓬老师 2017-04-10 15:27:53
[JavaScript讨论组]

为什么不足取整后还会出现这种现象?

<!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>
#p1{width:100px;height:100px;background:red;position:absolute;}
</style>


<script>
window.onload = function()
{
    var op = document.getElementById('p1');
    var oBtn= document.getElementById('btn');
    oBtn.onclick = move;
};
  var i=0;
function move()
{  i++;
   if(i%2==1){
     var timer = null
   timer = setInterval(function()
  {
        var op = document.getElementById('p1');
        var speed = (80-op.offsetLeft)/8;
        speed = speed > 0?Math.ceil(speed) : Math.floor(speed);
        op.style.left = op.offsetLeft + speed + 'px';
   
},30)}
   else{
    timerr=setInterval(function()
  {
        var op = document.getElementById('p1');
        var speed = (0-op.offsetLeft)/8;
        speed = speed > 0?Math.ceil(speed) : Math.floor(speed);
        op.style.left = op.offsetLeft + speed + 'px';
        document.title=op.offsetLeft+','+speed;
},30)
   }
      
    }
</script>


</head>

<body>
<input type="button" value="move" id="btn"/>


<p id="p1"></p>


</body>
</html>
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(1)
怪我咯

你是通过 i 来控制左右移动的。另外可能你对setInterval的理解有点偏差,如果你没有清除这个定时器,他将会一直执行,也就是说,你首次点击后第一个定时器会启动,而且一直执行下去,再点击一次,第二个也会启动,一个是对left进行加,一个进行减,所以导致抖动。修改后的代码如下:

<!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>
#p1{width:100px;height:100px;background:red;position:absolute;}
</style>




<script>
window.onload = function()
{
    var op = document.getElementById('p1');
    var oBtn= document.getElementById('btn');
    oBtn.onclick = move;
};
  var i=0;
  var timer = null
function move()
{   
   clearInterval(timer);
   i++;
   if(i%2==1){
     
   timer = setInterval(function()
  {
        var op = document.getElementById('p1');
        var speed = (80-op.offsetLeft)/8;
        speed = speed > 0?Math.ceil(speed) : Math.floor(speed);
        
        if (speed == 0) {clearInterval(timer);};
        
        op.style.left = op.offsetLeft + speed + 'px';
   
},30)}
   else{
       timer=setInterval(function()
  {
        var op = document.getElementById('p1');
        var speed = (0-op.offsetLeft)/8;
        speed = speed > 0?Math.ceil(speed) : Math.floor(speed);
        if (speed == 0) {clearInterval(timer);};
        op.style.left = op.offsetLeft + speed + 'px';
        document.title=op.offsetLeft+','+speed;
},30)
   }
      
    }
</script>




</head>

<body>
<input type="button" value="move" id="btn"/>




<p id="p1"></p>




</body>
</html>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号