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

    xiaoxiao2024-12-17  39

    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>
    最新回复(0)