来回运动

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .box {
            width: 200px;
            height: 200px;
            background-color: gold;
            position: absolute;
        }
    </style>
    <script type="application/javascript">
        window.onload = function () {
        var box1 = document.getElementById('box');
        var speed = 5;
        var ileft = 0;

        var timer = setInterval(function () {
            ileft += speed;
            box1.style.left = ileft + 'px';
            if (ileft > 800){
                speed = -5;
            }
            if (ileft < 0){
                speed = 5;
            }
        },30);
        }
    </script>
</head>
<body>
    <div class="box" id="box"></div>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/lurj/p/12463142.html