JavaScript点击让图片动起来

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        img {
            width: 200px;
            height: 200px;
        }
        div {
            position: absolute;
        }
    </style>
</head>
<body>
<input type="button" value="动起来" id="bt1"/>
<input type="button" value="停止" id="bt2"/>

<div id="dv">
    <img src="images/heihei.jpg" alt=""/>
    <img src="images/heihei.jpg" alt=""/>

</div>
<script src="common.js"></script>
<script>
    //点击按钮动起来
    my$("bt1").onclick = function () {
        timeId = setInterval(function () {
            //随机数
            var x = parseInt(Math.random() * 100 + 1);
            var y = parseInt(Math.random() * 100 + 1);
            my$("dv").style.left = x + "px";
            my$("dv").style.top = y + "px";
        }, 10);
    };
    //点击按钮停止
    my$("bt2").onclick = function () {
      clearInterval(timeId);
    };
</script>
</body>
</html>

 

posted @ 2018-08-01 14:31  {颜逸}  阅读(627)  评论(0编辑  收藏  举报