使用window.requestAnimationFrame制作动画

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xutongbao/article/details/82429148
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>使用window.requestAnimationFrame制作动画</title>
    <style type="text/css">
        .m-test{width: 100px;height: 100px;background-color: red;}
    </style>
</head>

<body>
    <div class="m-test" id="m-test"></div>
    <script type="text/javascript">
    var start = null;
    var element = document.getElementById('m-test');
    element.style.position = 'absolute';

    function step(timestamp) {
        if (!start) start = timestamp;
        var progress = timestamp - start;
        element.style.left = Math.min(progress / 10, 200) + 'px';
        if (progress < 2000) {
            window.requestAnimationFrame(step);
        }
    }

    window.requestAnimationFrame(step);
    </script>
</body>

</html>

猜你喜欢

转载自blog.csdn.net/xutongbao/article/details/82429148