javascript---在自由落体实现

实现一些简单的物业自由落体需要理解:

clientHeight:浏览器客户机的整体高度

offsetHeight:物(实例div)高低

offsetTop:从对象client最顶层的距离

简单demo例如以下:

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>free_movement</title>
	<style type="text/css">
		#div1{
			position: absolute;
			height: 100px;
			width: 100px;
			background: red;
		}
	</style>
	<script type="text/javascript">
		window.οnlοad=function  () {
			var btn=document.getElementById('btn');
			var div1=document.getElementById('div1');

			var Time=null;
			var speed=0;
			btn.οnclick=function () {
				startMove();
			}

			function startMove () {
				clearInterval(Time);
				Time=setInterval(function(){
					speed+= 3;
					var T = div1.offsetTop + speed;
					if(T > document.documentElement.clientHeight - div1.offsetHeight){
						T = document.documentElement.clientHeight - div1.offsetHeight;
						speed *= -1;
						speed *= 0.75;
					}
					div1.style.top=T+'px';
				}, 30)
			}
		}
	</script>
</head>
<body>
	<input type='button' value='開始运动' id="btn">
	<div id="div1"></div>
</body>
</html>

注:clearTnterval(Time)://防止多个点击事件



版权声明:本文博主原创文章。博客,未经同意不得转载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值