js 匀速运动重新复习

该博客探讨了如何使用CSS实现元素的平滑移动动画。通过调整元素的left属性,实现了从一个位置到另一个位置的匀速运动。文章特别强调了offsetLeft属性只能读取不可设置,以及在处理动画过程中遇到的边界问题。通过代码优化,解决了不同方向运动的处理,并确保了动画的平滑性。
摘要由CSDN通过智能技术生成
<!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>Document</title>

    <style>
        .box {
            position: absolute;
            left: 700px;
            top: 0;
            width: 100px;
            height: 100px;
            background-color: #ccc;
        }
        
        span {
            position: absolute;
            width: 1px;
            height: 500px;
            left: 300px;
            top: 0px;
            background-color: red;
        }
        
        button {
            margin-top: 200px;
        }
    </style>
</head>

<body>


    <div class="box">


    </div>
    <span></span>
    <button id="move">move</button>
    <script>
        window.onload = function() {
            var oBox = document.querySelector(".box");
            var oButton = document.querySelector("#move");
            var speed = 13;
            var target = 300;
            oButton.addEventListener("click", function() {
                // 根据左右位置计算速度
                var speed = target > oBox.offsetLeft ? 7 : -7;
                oBox.timer = setInterval(function() {

                    // offsetLeft 的值是只读属性,不能被修改,
                    // 所以说left 始终等于 0px ,所以动画失效!
                    // oBox.offsetLeft += speed;
                    // oBox.style.left = oBox.offsetLeft + "px";

                    //修改如下操作
                    var left = oBox.offsetLeft + speed;

                    // 边界处理问题!
                    // if (speed > 0) {
                    //     // 解决边界处理问题!
                    //     if (left >= target) {
                    //         clearInterval(oBox.timer)
                    //         oBox.timer = null;
                    //         oBox.style.left = target + "px";
                    //     } else {
                    //         oBox.style.left = left + "px";
                    //     }
                    // } else {
                    //     if (left <= target) {
                    //         clearInterval(oBox.timer)
                    //         oBox.timer = null;
                    //         oBox.style.left = target + "px";
                    //     } else {
                    //         oBox.style.left = left + "px";
                    //     }
                    // }

                    // 对上面代码进行优化处理

                    if (Math.abs(left - target) < Math.abs(speed)) {
                        clearInterval(oBox.timer)
                        oBox.timer = null;
                        oBox.style.left = target + "px";
                    } else {
                        oBox.style.left = left + "px";
                    }


                }, 50)

            })
        }
    </script>
</body>

</html>

 

重点, 1 offsetLeft 值不能设置,只能读取

2, 匀速运动,有两个方向,要进行分别处理

3, 当我们分别处理后,发现,代码没有多大区别,这个时候,要进行代码合并操作

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值