Blogger Information
Blog 8
fans 0
comment 0
visits 7087
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
小案例用小球移动2019年1月21日
A云海的博客
Original
673 people have browsed it

总结:

1、position: relative;相对位置进行移动;

2、用setInterval() 方法设置定时器,会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭;

3、 ball.style.left = ball.style.top = count + 'px'; 来设置向左和上进行移动;

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<style>
   div{
       border: 2px solid #000;
       height: 40px;
       width: 40px;
       border-radius: 40px;
       margin: 50px 10px;
       background-color: #000;
       box-shadow: 2px 2px 2px #000;
       position: relative;
   }
</style>
<body>
    
    <button type="button" onclick="myBall()">点击</button>
    <div id="ball"></div>
    <script>
    function myBall() {
        var ball = document.getElementById("ball");   
        var count = 0;
        var id = setInterval(frame, 10);
        function frame() {
            if (count <  100) {
                cont++; 
                ball.style.left = ball.style.top = count + 'px'; 
            } else {
            clearInterval(id);
            }
        }
    }
    </script>
 
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post