zoukankan      html  css  js  c++  java
  • js动画

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <title>Document</title>
            <style>
                #block {
                    position: absolute;
                    width: 100px;
                    height: 100px;
                    background-color: red;
                    top: 0;
                    left: 0;
                }
            </style>
        </head>
        <body>
            <div id="block"></div>
            <script type="text/javascript">
                window.onload = function () {
                    // API实现
                    const requestAnimationFrameHandle = () => {
                        const element = document.getElementById('block');
                        let start = null;
                        let animate = null;
                        const step = timestamp => {
                            if (!start) start = timestamp;
                            const progress = timestamp - start;
                            element.style.left =
                                Math.min(progress / 10, 500) + 'px';
                            if (progress < 5000) {
                                animate = window.requestAnimationFrame(step);
                            } else {
                                window.cancelAnimationFrame(animate);
                            }
                        };
                        animate = window.requestAnimationFrame(step);
                    };
                    requestAnimationFrameHandle();
                    // 定时器实现
                    const timeAnimation = () => {
                        const element = document.getElementById('block');
                        let progress = 0;
                        let timer = setInterval(() => {
                            element.style.left =
                                Math.min(progress / 10, 500) + 'px';
                            progress = progress + 10;
                            if (progress > 5000) {
                                clearInterval(timer);
                                timer = null;
                            }
                        }, 10);
                    };
                    timeAnimation();
                };
            </script>
        </body>
    </html>
  • 相关阅读:
    JAVA视频网盘分享
    IntelliJ IDEA 2016 完美破解+汉化补丁
    献给java求职路上的你们
    java集合类
    MyEclipse编码设置
    国内外有名的java论坛
    百度编辑器ueditor的简单使用
    工厂模式
    Java静态工厂的理解
    git clone index-pack failed
  • 原文地址:https://www.cnblogs.com/tllw/p/12888916.html
Copyright © 2011-2022 走看看