Advertisement

JS+HTML+CSS实现动画移动效果

阅读量:

JS+HTML+CSS实现动画移动效果

效果如图所示

在这里插入图片描述
复制代码
    <!DOCTYPE html>
    <html lang="en">
    <style>
    #big{
        width: 400px;
        height: 400px;
        background-color: green;
        position: relative;//相对定位
    }
    #small{
        width: 30px;
        height: 30px;
        background-color: red;
        position: absolute;//绝对定位
    }
    </style>
    <head>
    <meta charset="UTF-8">
    <title>动画简单显示</title>
    
    </head>
    <body>
    <p><button onclick="move()">开始动画</button></p>
    <div id="big">
        <div id="small"></div>
    </div>
    <script>
        function move() {
            var a=document.getElementById("small");
            var id=setInterval(frame,5);//无限循环
            var pos=0;
            function frame() {
                if (pos==370){
                    clearInterval(id);//清理无限循环
                } else {
                    pos++;
                    a.style.top=pos+"px";//设置每次的新位置
                    a.style.left=pos+"px";
                }
    
            }
        }
    </script>
    </body>
    </html>
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

全部评论 (0)

还没有任何评论哟~