阅读背景:

使用JavaScript左右移动方框

来源:互联网 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>左右移动方框</title>
    <script>
        window.onload=function(){  
            var box = document.getElementById('box');
            var iLeft = 0;
            var Speed = 3;         
            //定义一个定时器
            var Timer = setInterval(Move,5);
            
            function Move(){
                iLeft +=Speed;

                if (iLeft>1300){
                    Speed = -3
                }
                if(iLeft<0){
                    Speed = 3                
                }
                box.style.left = iLeft + 'px'
            }
           

              
        }
    
    </script>
    <style>
        #box {
            width: 100px;
            height: 100px;
            background-color: hotpink;
            position: fixed;
        }    
    </style>
</head>
<body>

    <div id="box"></div>

</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
    <me



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: