这个效果实现一个反弹的效果,在很多场合都可以使用哦
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 
  | <!doctype html> <html> 	<head> 		<meta charset="utf-8"/> 		<title>碰壁反弹</title> 		<style> 			#wrap{ 				width:1200px; 				height:800px; 				border:8px green solid; 				position: relative; 			} 			#inner{ 				width:200px; 				height:200px; 				background:black; 				border:10px yellow solid; 				position: absolute; 				top:0; 				left:0; 			} 		</style> 	</head> 	<body> 		<div id="wrap"> 			<div id="inner"></div> 		</div> 	</body> 	<script> 		var oWrap=document.getElementById('wrap'); 		var oInner=document.getElementById('inner'); 		var speed=10;//定义速度 		// 最大横向移动距离; 		var Max_Width=oWrap.clientWidth-oInner.offsetWidth; 		//  最大纵向移动距离; 		var Max_Height=oWrap.clientHeight-oInner.offsetHeight; 		// 开关功能 		var x=y=true; 		// 测试 0; 		console.log(oInner.offsetLeft); 			// 加定时器,每50s循环一次; 			var timer=null; 			timer=setInterval(function(){ 				// 存变量 				var ll=oInner.offsetLeft; 				var tt=oInner.offsetTop; 				if(x){ 					ll+=speed; 					if(ll>Max_Width){ 						x=false; 					} 				} 				else{ 					ll-=speed; 					if(ll<0){ 						x=true; 					} 				} 				if(y){ 					tt+=speed; 					if(tt>Max_Height){ 						y=false; 					} 				} 				else{ 					tt-=speed; 					if(tt<0){ 						y=true; 					} 				} 				// 让left和top随着offsetLeft和offsetTop运动; 				oInner.style.left=ll+"px"; 				oInner.style.top=tt+"px"; 			},50) 	</script> </html> 
  | 
 
你也可以尝试着用canvas做哦