首页 > Web开发 > 详细

JS抛物线运动

时间:2017-01-09 20:13:54      阅读:302      评论:0      收藏:0      [点我收藏+]

<!DOCTYPE html>
<html>
    <head>
        <meta charset="{CHARSET}">
        <title></title>
        <style type="text/css">
            #box{background: greenyellow;
                border-radius: 50%;
                width: 90px;
                height: 90px;
                position: absolute;
                left: 20px;
                top: 200px;                
                }
        </style>
        <script type="text/javascript">
            onload=function(){
                var oBox=document.getElementById("box");
                //给一个速度
                //水平速度
                var xSpeed=10;
                //垂直速度
                var ySpeed=-30;
                var timer=setInterval(function(){
                    ySpeed+=3;
                    oBox.style.left=xSpeed+oBox.offsetLeft+"px";
                    oBox.style.top=ySpeed+oBox.offsetTop+"px";
                    //边界判断
                    var clientHeight=document.documentElement.clientHeight||document.body.clientHeight;
                    if (oBox.offsetTop>=clientHeight-oBox.offsetHeight) {
                        //修正位置,正好触底
                        oBox.style.top=clientHeight-oBox.offsetHeight+"px";
                        //修正y的速度,使其向相反的方向运动,并且速度越来越小
                        ySpeed *= -0.6;
                    }
                },100)
                //处理一下水平方向的速度,让其越来越慢,最后变为0
                var timer2 = setInterval(function(){
                    --xSpeed <=0 ? clearInterval(timer2) : "";
                    
                },2000);
                
                
            }
        </script>
    </head>
    <body>
        <div id="box" >
            
        </div>
    </body>
</html>

JS抛物线运动

原文:http://www.cnblogs.com/qingxh/p/6266320.html

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