zoukankan      html  css  js  c++  java
  • javaScript运动框架之缓冲运动

    缓冲运动

      逐渐变慢,最后停止

      距离越远速度越大

          速度由距离决定

        速度=(目标值-当前值)/缩放系数

         存在Bug

        速度取整

        跟随页面滚动的缓冲侧边栏

           潜在问题:目标值不是整数时

    缓冲运动的停止条件

      运动终止条件:两点重合(即运动物体和目的地重合)

    Demo代码

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>缓冲运动</title>
        <style>
            .sport {
                width: 60px;
                height: 60px;
                background-color: #FC4209;
                border-radius: 5px;
                position: absolute;
                top: 60px;
                text-align: center;
                line-height: 60px;
            }
    
            #div1 {
                top: 60px;
                left: 0;
            }
    
            #div2 {
                top: 150px;
                left: 900px;
            }
    
            #reference {
                width: 1px;
                height: 260px;
                background-color: black;
                position: fixed;
                top: 20px;
                left: 450px;
            }
        </style>
        <script>
            function startMove(obj, iTarget) {
                let timer = null;
                let oDiv1 = document.getElementById(obj);
    
                clearInterval(timer);
                timer = setInterval(function () {
                    let iSpeed = (iTarget - oDiv1.offsetLeft) / 12;
                    // Math.ceil() 向上取整   Math.floor() 向下取整 
                    iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
    
                    if (oDiv1.offsetLeft == iTarget) {
                        clearInterval(timer);
                    } else {
                        oDiv1.style.left = oDiv1.offsetLeft + iSpeed + 'px';
                    }
                }, 30);
            }
        </script>
    </head>
    
    <body>
        <input type="button" value="开始运动" onclick="startMove('div1',450)" />
        <input type="button" value="开始运动" onclick="startMove('div2',450)" />
        <div id="div1" class="sport">A</div>
        <div id="div2" class="sport">B</div>
        <div id="reference"></div>
    </body>
    
    </html>

     

    效果图A:

    效果图B:

     

     

    路漫漫其修远兮,吾将上下而求索
  • 相关阅读:
    性能调优利器之strace
    如何写出优雅的Python(二)
    c# 模拟 网页实现12306登陆、自动刷票、自动抢票完全篇
    使用Javascript无限添加QQ好友原理解析
    微信公众账号开发之微信登陆Oauth授权-第一篇
    WPF下的仿QQ图片查看器
    不用写软件,纯JS 实现QQ空间自动点赞
    软件分层架构下的另类设计框架-工厂模式的使用
    Javascript实现Linq查询方式
    c# 使用正则表达式 提取章节小说正文全本篇
  • 原文地址:https://www.cnblogs.com/jsanntq/p/7696009.html
Copyright © 2011-2022 走看看