JavaScript div移动动画


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        div {
            margin-top: 10px;
            width: 100px;
            height: 50px;
            background-color: purple;
            position: absolute;
        }
    </style>
</head>
<body>
<input type="button" value="移动到400px" id="bt1"/>
<input type="button" value="移动到800px" id="bt2"/>

<div id="dv"></div>
<script src="common.js"></script>
<script>
    my$("bt1").onclick = function () {
        //获取div当前的left值
        var left = my$("dv").offsetLeft;
        var step = 10;
        timeId = setInterval(function () {
            if (left <= 400) {
                left += step;
                my$("dv").style.left = left + "px";
                console.log(left);
            } else {
                clearInterval(timeId);
            }
        }, 10)
    };
    my$("bt2").onclick = function () {
        //获取div当前的left值
        var left = my$("dv").offsetLeft;
        var step = 10;
        timeId = setInterval(function () {
            if (left <= 800) {
                left += step;
                my$("dv").style.left = left + "px";
                console.log(left);
            } else {
                clearInterval(timeId);
            }
        }, 10)
    };
</script>
</body>
</html>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM