缓冲运动封装

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        div {
            width: 100px;
            height: 200px;
            background-color: rgb(137, 137, 221);
            position: absolute;
            top: 200px;
            left: 0;
        }
        body {
            height: 2000px;
        }
    </style>
</head>

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

</html>
<script>
    let odiv = document.querySelector("div");
    let time = null;
</script>
<script src="缓冲运动.js"></script>

缓冲运动.js

function move(obj, target) {
    clearInterval(time);
    time =setInterval(function () {
        let speed = (target - obj.offsetTop) / 10;
        speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
        obj.style.top = obj.offsetTop + speed + "px";
        if (obj.offsetLeft == target) {
            clearInterval(time);
        }
    }, 50)

}
window.onscroll = function () {
    let _top = document.body.onscrollTop || document.documentElement.scrollTop;
    move(odiv, _top+50);
}
上一篇:视频聊天源码的jQuery 效果, 隐藏和显示


下一篇:05-pygame弹球游戏(上)