js实现缓冲运动

23 篇文章 0 订阅

<!DOCTYPE  HTML>
<html >
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>缓冲运动</title>
  <style type="text/css">
  *{
    padding:0;
    margin:0;
  }
  #div1{
    width:200px;
    height:200px;
    background: red;
    position: relative;
    left: -200px;
    top: 0;
  }
  #div1 span{
    width: 20px;
    height: 50px;
    background: blue;
    position: absolute;
    left: 200px;
    top:75px;
  }
</style>
<!-- 缓冲运动
  1、获取运动对象的offsetLeft
  设置定时器
  2、定义运动速度(*)
  3、改变运动对象的offsetLeft
  4、临界值判断(*)

-->
<script type="text/javascript">
  window.onload = function(){
    var div1 = document.getElementById('div1');
    div1.onmouseover = function(){
      move(div1,0);
    }

    div1.onmouseout = function(){
      move(div1,-200);
    }
  }
  var timer = null;
  var speed = 0;
    /*
      obj运动对象
      stopPoint停止点
      */
      function move(obj,stopPoint){
        //var div1 = document.getElementById('div1');
        clearInterval(timer);
        timer = setInterval(function(){

          if(stopPoint >= 0){
            speed = Math.ceil((stopPoint-obj.offsetLeft)/7);
          }else{
            speed = Math.floor((stopPoint-obj.offsetLeft)/7);
          }
          console.log(speed);

              //临界值判断,当运动到临界值时停止 清除定时器 
            if(obj.offsetLeft == stopPoint){ 
              clearInterval(timer);
            }else{
                obj.style.left = obj.offsetLeft+speed+'px'; //运动
              }
              
            },30);
      }
    </script>
  </head>
  <body>
    <div id="div1">
      <span id="share">分享</span>
    </div>

  </div>
</body>
</html>

匀速运动的时候就是固定运动速度的值

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现盒子大小有缓冲的效果,可以使用JavaScript的`setInterval`函数和缓动算法。 以下是一个简单的实现示例: HTML: ```html <div id="box"></div> <button onclick="changeBoxSize()">改变盒子大小</button> ``` CSS: ```css #box { width: 100px; height: 100px; background-color: red; transition: all 0.5s ease-out; } ``` JavaScript: ```javascript function changeBoxSize() { var box = document.getElementById("box"); var targetWidth = Math.floor(Math.random() * 300) + 50; var targetHeight = Math.floor(Math.random() * 300) + 50; var interval = setInterval(function() { var currentWidth = box.offsetWidth; var currentHeight = box.offsetHeight; var distanceWidth = targetWidth - currentWidth; var distanceHeight = targetHeight - currentHeight; var speedWidth = distanceWidth / 10; var speedHeight = distanceHeight / 10; if (Math.abs(distanceWidth) < 1 && Math.abs(distanceHeight) < 1) { clearInterval(interval); } else { box.style.width = currentWidth + speedWidth + "px"; box.style.height = currentHeight + speedHeight + "px"; } }, 20); } ``` 在这个示例中,当点击按钮时,`changeBoxSize`函数会被调用。这个函数会使用`setInterval`函数来逐步改变盒子的大小,直到达到目标大小。在每个时间间隔中,我们会计算当前大小和目标大小之间的距离,并根据距离计算出盒子每次缩放的速度,然后用这个速度逐步改变盒子的大小。这个过程会在`setInterval`函数中一直进行,直到盒子的大小达到目标大小,然后我们会使用`clearInterval`函数停止这个过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值