div重复移动

<html>
<head>
    <title>上下左右移动</title>
    <style>
        #div1 {
            position: absolute;
            border: 1px solid black;
            width: 50px;
            height: 50px;
            left: 0px;
            top: 100px;
        }


        #div2 {
            position: absolute;
            border: 1px solid black;
            width: 50px;
            height: 50px;
            right: 0px;
            top: 100px;
        }


        #div3 {
            position: absolute;
            border:  solid 1px black;
            width: 50px;
            height: 50px;
            left: 20px;
            bottom:0px;
        }


        #div4 {
            position: absolute;
            border:  solid 1px black;
            width: 50px;
            height: 50px;
            right: 20px;
            top: 0px;
        }
    </style>
    <script>
        var timer1 = null;


        function moveItRight(val) {
            var el = document.getElementById("div1");
            if (parseInt(el.style.left) > (val))
                el.style.left = 0;
            el.style.left = parseInt(el.style.left) + 2 + 'px';
            timer1 = setTimeout(moveItRight, 25, 200);   // 在指定的毫秒数后调用函数
        }


        function moveItLeft(val) {
            var el = document.getElementById("div2");
            if (parseInt(el.style.right) > (val))
                el.style.right = 0;
            el.style.right = parseInt(el.style.right) + 2 + 'px';
            timer1 = setTimeout(moveItLeft, 25, 200);   // 在指定的毫秒数后调用函数
        }


        function moveItUp(val) {
            var el = document.getElementById("div3");
            if (parseInt(el.style.bottom) > (val))
                el.style.bottom = 0;
            el.style.bottom = parseInt(el.style.bottom) + 2 + 'px';
            timer1 = setTimeout(moveItUp, 25, 200);   // 在指定的毫秒数后调用函数
        }


        function moveItDown(val) {
            var el = document.getElementById("div4");
            if (parseInt(el.style.top) > (val))
                el.style.top = 0;
            el.style.top = parseInt(el.style.top) + 2 + 'px';
            timer1 = setTimeout(moveItDown, 25, 200);   // 在指定的毫秒数后调用函数
        }




        window.onload = function () {


            document.getElementById("div1").style.left = 0;
            moveItRight(200);
            document.getElementById("div2").style.right = 0;
            moveItLeft(200);
            document.getElementById("div3").style.bottom = 0;
            moveItUp(200);
            document.getElementById("div4").style.top = 0;
            moveItDown(200);
        }
    </script>
</head>
<body>
    <div id="div1" style="top:323;bottom"> Move left! </div>


    <div id="div2"> Move right! </div>




    <div id="div3">Move up!</div>


    <div id="div4">Move down!</div>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值