js运动技术--链式运动框架

其原理是函数startMove多了一个参数,这个参数是一个函数,可以在定时器结束时调用。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			#div1{height: 200px;width: 200px;background: red;margin: 20px;float: left;left: 20px; top: 20px;
			border: 10px solid black;
			filter: alpha(opacity;30);opacity: 0.3;}
		</style>
		<script>
			function getStyle(obj,name){
				if(obj.currentStyle){
					return obj.currentStyle[name];
				}
				else{
					return getComputedStyle(obj,false)[name];
				}
			}
			
			
			function startMove(obj,style1,iTarget,fnEnd){
				var oTxt = document.getElementById("txt");		
				clearInterval(obj.timer);
		
				obj.timer = setInterval(function(){
					var cur = 0;
					if(style1 == 'opacity'){
						 cur = parseFloat(getStyle(obj,style1))*100;
					}
					else {
						cur = parseInt(getStyle(obj,style1));
					}
					
					var speed = (iTarget - cur) / 6;
					speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
					
					oTxt.value = cur;
					
					if(iTarget == cur){
						clearInterval(obj.timer);
						
						if(fnEnd)fnEnd();			//传递了函数则执行;		
					}
					else {
						if(style1 == 'opacity'){
							obj.style.filter = 'alpha(opacity:'+(cur+speed)+')';
							obj.style.opacity = (cur+speed) / 100;
						}
						else{
							obj.style[style1] = cur + speed + 'px';
						}
					}
				},30);
			}
			
			
			window.onclick = function(){
				var oDiv1 = document.getElementById("div1");
				oDiv1.onmouseover = function(){
					startMove(oDiv1,'width',400,function(){
						startMove(oDiv1,'height',400,function(){
							startMove(oDiv1,'opacity',100);
						});
					});
				}
				oDiv1.onmouseout = function(){
					startMove(oDiv1,'opacity',30,function(){
						startMove(oDiv1,'height',200,function(){
							startMove(oDiv1,'width',200);
						})
					});
				}
			}
		</script>
	</head>
	<body>
		<input type = "text" id = "txt"/><br />
		<div id = "div1">改变透明度</div>
	</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏油

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值