首页 > 移动平台 > 详细

JavaScript div移动动画

时间:2018-08-02 15:26:48      阅读:165      评论:0      收藏:0      [点我收藏+]
<!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>

 

JavaScript div移动动画

原文:https://www.cnblogs.com/cuilichao/p/9407272.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!