平抛与拍球

平抛运动,水平方向一个速度,垂直方向一个速度,垂直方向速度依次变大,水平方向速度不变

拍球:当鼠标移到球上时,速度变大、

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>重力运动</title>
<style type="text/css">
    *{
        padding: 0px;
        margin:0px;
    }
    #box{
        100%;
        height:500px;
        border-bottom: 3px solid black;
        position: relative;
    }
    #img{
        position: absolute;
        top:25px;
        left:30px;
    }
</style>
<script type="text/javascript">
window.onload=function(){
    var oqiu=document.getElementById('img');
    var obtn=document.getElementById('btn');
    var obtn1=document.getElementById('btn1');

    obtn.onclick=function(){
        var speed=1;//设置速度变量
        timer=setInterval(function(){//定时器让小球动起来
            oqiu.onmouseover=function(){//当鼠标移到小球上,速度变大
                speed=Math.abs(speed)*(1.3);
            }
            speed+=3;
            var old_top=oqiu.offsetTop;//获得就得top值
            var new_top=old_top+speed;//计算新的top值

            var old_left=oqiu.offsetLeft;//获得水平方向的left值
            var new_left=old_left+5;//水平方向速度不变

            if(new_top>400){
                new_top=400;
                speed*=-0.80;//速度损失,并改变方向
            }
            if(-1<speed&&speed<1){
                speed=0;//当速度特别小时,直接变为0,防止抖动
                
            }
            if(speed==0&&new_top==400){
                clearInterval(timer);//当速度为0而且达到最大速度,关掉定时器
            }
            oqiu.style.top=new_top+'px';//赋值
            oqiu.style.left=new_left+'px';//赋值
            
        },15);

    }
    obtn1.onclick=function(){
        oqiu.style.top=25+'px';//按恢复键,球变到初始位置
        clearInterval(timer);
    }
    
}
</script>
</head>
<body>
<div id="box">
    <img src="qiu.jpg" alt="" id="img">
    <input type="button" id="btn" value="走你">
    <input type="button" id="btn1" value="恢复">
</div>    
</body>
</html>
原文地址:https://www.cnblogs.com/lzzhuany/p/4584513.html