js 實現多物體動畫效果

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			ul,li{
				list-style: none;
			}
			ul li{
				width: 200px;
				height: 100px;
				background: yellow;//設置背景顏色
				margin-bottom: 20px;
			}
			
		</style>
		<script>
			window.onload=function(){
				var aLi=document.getElementsByTagName('li');
				for(var i=0;i<aLi.length;i++){
					aLi[i].onmouseover=function(){
						startMove(this,400);
					}
					aLi[i].onmouseout=function(){
						startMove(this,200);
					}
				}
			}
			var timer=null;//設置定時器
			function startMove(obj,iTarget){
				clearInterval(timer);
				timer=setInterval(function(){
					var speed=(iTarget-obj.offsetWidth)/8;
					speed=speed>0?Math.ceil(speed):Math.floor(speed);
					if(obj.offsetWidth==iTarget){
						clearInterval(timer);
					}
					else{
						obj.style.width=obj.offsetWidth+speed+'px';
					}
				},30)
				
			}
		</script>
	</head>
	<body>
		<ul>
			<li></li>
			<li></li>
			<li></li>
		</ul>
	</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章