[HTML/JS]使用JS实现水平缓动动画

文章提供了一个使用JavaScript实现的水平位移动画代码,该动画适用于滚动巨幕和公告更迭等场景。通过设置CSS固定定位,然后利用JavaScript监听点击事件,计算并更新div元素的left属性,以平滑移动到目标位置。代码中还包含清除定时器和防止过度移动的逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

水平位移动画能用在很多地方,如滚动巨幕,公告更迭等。具体效果及代码如下。

代码如下

样式部分

div {
        position: fixed;
        width: 100px;
        height: 100px;
        background-color: yellow;
        left: 0;
        top: 100px;
    }

主体部分

 <input type="text">
    <div>

    </div>

JS部分

<script>

        window.onload = function (dom, target) {
            var input = document.querySelector('input')
            var div = document.querySelector('div')
            div.onclick = function () {
                animate(div, input.value);
                console.log(input.value);
            }
            function animate(dom, target) {
                clearInterval(dom.timer)
                console.log("dom.timer",dom.timer);

                dom.timer = setInterval(function () { 

                    var nowLeft = dom.offsetLeft;

                    var need = (target - dom.offsetLeft)/10;
                    need = need > 0?Math.ceil(need) : Math.floor(need);
                    // (var need = nowLeft< target ? 3 : -3;
                    // console.log(need);)

                    var targetLeft = nowLeft + need;
                    // console.log(targetLeft);

                    dom.style.left = targetLeft + "px";

                    if (Math.abs(targetLeft - target) <= Math.abs(need)) {
                        clearInterval(dom.timer);
                        console.log('结束定时器');
                        dom.style.left = target + "px";
                    }
                }, 50)
            }
        }
    </script>

<script>

 //window.onload使JS可位于主体后

        window.onload = function (dom, target) {

//获取input和div节点

            var input = document.querySelector('input')

            var div = document.querySelector('div')

//为div添加点击事件

            div.onclick = function () {

//调用animate(div,input,value)事件

                animate(div, input.value);

               // console.log(input.value);

            }

//animate事件

            function animate(dom, target) {

//清除dom.timer计时器

                clearInterval(dom.timer)

                console.log("dom.timer",dom.timer);

//dom.timer计时器

                dom.timer = setInterval(function () {

//获取初始位置

                    var nowLeft = dom.offsetLeft;

//目标位置-当前位置的差除以10得到每次移动的距离

                    var need = (target - dom.offsetLeft)/10;

//判断后为”每步长度“取整

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

//获取每次位移后的位置

                    var targetLeft = nowLeft + need;

                    dom.style.left = targetLeft + "px";

//Math.abs取整

//判断与目标距离小于或等于“每步长度”时直接抵达目标终点

                    if (Math.abs(targetLeft - target) <= Math.abs(need)) {

                        clearInterval(dom.timer);

                        console.log('结束定时器');

                        dom.style.left = target + "px";

                    }

//间隔50毫秒

                }, 50)

            }

        }

    </script>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值