首页 > 其他 > 详细

简单的碰撞运动

时间:2016-11-27 20:16:46      阅读:235      评论:0      收藏:0      [点我收藏+]

需要的js
//碰撞运动
//对运动的方向以及临界值的处理
function bumpMove(obj) {
    clearInterval(obj.timer);
    var speedX = 10;
    var speedY = 10;
    timer = setInterval(function() {
        var L = obj.offsetLeft + speedX;
        var T = obj.offsetTop + speedY;

        if(T > document.documentElement.clientHeight - obj.offsetHeight) {
            T = document.documentElement.clientHeight - obj.offsetHeight;
            speedY *= -1;
        } else if(T < 0) {
            T = 0;
            speedY *= -1;
        }
        if(L > document.documentElement.clientWidth - obj.offsetWidth) {
            L = document.documentElement.clientWidth - obj.offsetWidth;
            speedX *= -1;
        } else if(L < 0) {
            L = 0;
            speedX *= -1;
        }

        obj.style.left = L + ‘px‘;
        obj.style.top = T + ‘px‘;
    }, 30);

}

 



<!
DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" type="text/css" href="../css/public.css"/> <style type="text/css"> #div1{width: 100px;height: 100px;background: red;position: absolute;} </style> <script type="text/javascript" src="../js/rainbow.js"></script> <script type="text/javascript"> window.onload=function(){ var oDiv=document.getElementById("div1"); bumpMove(oDiv); } </script> </head> <body> <div id="div1"></div> </body> </html>

 

简单的碰撞运动

原文:http://www.cnblogs.com/rain92/p/6106909.html

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