`
ch_kexin
  • 浏览: 877251 次
  • 性别: Icon_minigender_2
  • 来自: 青岛
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
简单的动画
第一个版本:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>动画</title>
<script type="text/javascript">
window.onload=function(){
	var Teen = {  
		Cubic: {                
					easeIn: function(initPos, targetPos, currentCount, count) {
						       var b = initPos, c = targetPos - initPos, t = currentCount, d = count;                    
							   return c * (t /= d) * t * t + b;                
				    },                
					easeOut: function(initPos, targetPos, currentCount, count) {                    
								var b = initPos, c = targetPos - initPos, t = currentCount, d = count;                    
								return c * ((t = t / d - 1) * t * t + 1) + b;                
					},                
					easeInOut: function(initPos, targetPos, currentCount, count) {                    
								var b = initPos, c = targetPos - initPos, t = currentCount, d = count;                    
								if ((t =t/(d/2)) < 1) return c / 2 * t * t * t + b;                    
								return c / 2 * ((t -= 2) * t * t + 2) + b;                
					}            
			}
			
	}
	var animateFun = function(ele,styleName,to,count,timespan){
		 var timespan = !!timespan?timespan:10;
		 var count =!!count?count:100;
		 var curAnimateFun = Teen.Cubic.easeIn;
		 var curCount = 0;
		 var styleValue= ele.style[styleName]||0;
		 styleValue =parseInt(styleValue);
		 var curInterval = window.setInterval(function(){
			if(curCount>count){
				window.clearInterval(curInterval);
			}else{
				curCount=curCount+1;
				var tempValue = curAnimateFun(styleValue,to,curCount,count);
				//alert(tempValue);
				ele.style[styleName] = tempValue+"px";
			}
			
		 },timespan);
	}
	
	document.getElementById("btn").onclick=function(){
		//animateFun(document.getElementById("res"),'top',300);
	}
	document.getElementById("btn1").onclick=function(){
		//animateFun(document.getElementById("res"),'top',0);
	}
}
</script>
</head>

<body>
<div id="res" style="width:100px; height:100px; background-color:#339966; position:absolute; top:0; left:0"></div>
<input type="button" style="margin-top:200px;" value="动起来"  id="btn"/>
<input type="button" style="margin-top:200px; margin-left:50px;" id="btn1" value="回去"/>
</body>
</html>


第二个版本
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>动画</title>
<script type="text/javascript">
window.onload=function(){
	var Teen = {  
		Cubic: {                
					easeIn: function(initPos, targetPos, currentCount, count) {
						       var b = initPos, c = targetPos - initPos, t = currentCount, d = count;                    
							   return c * (t /= d) * t * t + b;                
				    },                
					easeOut: function(initPos, targetPos, currentCount, count) {                    
								var b = initPos, c = targetPos - initPos, t = currentCount, d = count;                    
								return c * ((t = t / d - 1) * t * t + 1) + b;                
					},                
					easeInOut: function(initPos, targetPos, currentCount, count) {                    
								var b = initPos, c = targetPos - initPos, t = currentCount, d = count;                    
								if ((t =t/(d/2)) < 1) return c / 2 * t * t * t + b;                    
								return c / 2 * ((t -= 2) * t * t + 2) + b;                
					}            
			}
	}
	var animateFun = function(ele,styleName,to,call,count,timespan){
		 var timespan = !!timespan?timespan:5;
		 var count =!!count?count:50;
		 var curAnimateFun = Teen.Cubic.easeIn;
		 var curCount = 0;
		 var styleValue= ele.style[styleName]||0;
		 var callfun = call;
		 styleValue =parseInt(styleValue);
		 var curInterval = window.setInterval(function(){
			if(curCount>count){
				window.clearInterval(curInterval);
				if(!!callfun){
					call();
				}
			}else{
				curCount=curCount+1;
				var tempValue = curAnimateFun(styleValue,to,curCount,count);
				ele.style[styleName] = tempValue+"px";
			}
			
		 },timespan);
		}
		var call1 = function(){
			alert(123);
		}
		document.getElementById("btn").onclick= function(){
				animateFun(document.getElementById("res"),'left',300,call1);
		}
		document.getElementById("btn1").onclick= function(){
				animateFun(document.getElementById("res"),'left',0,call1);
		}
	
	
	
	
}
</script>
</head>

<body>
<div id="res" style="width:100px; height:100px; background-color:#339966; position:absolute; top:0; left:0"></div>
<input type="button" style="margin-top:200px;" value="动起来"  id="btn"/>
<input type="button" style="margin-top:200px; margin-left:50px;" id="btn1" value="回去"/>
</body>
</html>

第三个版本:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>动画</title>
<script type="text/javascript">
window.onload=function(){
	var Teen = {  
		Cubic: {                
					easeIn: function(initPos, targetPos, currentCount, count) {
						       var b = initPos, c = targetPos - initPos, t = currentCount, d = count;                    
							   return c * (t /= d) * t * t + b;                
				    },                
					easeOut: function(initPos, targetPos, currentCount, count) {                    
								var b = initPos, c = targetPos - initPos, t = currentCount, d = count;                    
								return c * ((t = t / d - 1) * t * t + 1) + b;                
					},                
					easeInOut: function(initPos, targetPos, currentCount, count) {                    
								var b = initPos, c = targetPos - initPos, t = currentCount, d = count;                    
								if ((t =t/(d/2)) < 1) return c / 2 * t * t * t + b;                    
								return c / 2 * ((t -= 2) * t * t + 2) + b;                
					}            
			}
	}
	var animateFun = function(){
		 var len = arguments.length;
		 var ele,styleName,to,count,timespan,call;
		 if(len>3){
			//如果大于3个参数,那么
			ele = arguments[0];
			styleName=arguments[1];
			to = arguments[2];
			for(var i =3;i<len;i++){
				if(typeof(arguments[i])=="function"){
					call = arguments[i];
				}else{
					if(!!!count){
						count = arguments[i];
					}else{
						timespan = arguments[i];
					}
				}
				
			}	
		 }
		 var timespan = !!timespan?timespan:5;
		 var count =!!count?count:50;
		 var curAnimateFun = Teen.Cubic.easeIn;
		 var curCount = 0;
		 var styleValue= ele.style[styleName]||0;
		 var callfun = call;
		 styleValue =parseInt(styleValue);
		 var curInterval = window.setInterval(function(){
			if(curCount>count){
				window.clearInterval(curInterval);
				if(!!callfun){
					call();
				}
			}else{
				curCount=curCount+1;
				var tempValue = curAnimateFun(styleValue,to,curCount,count);
				ele.style[styleName] = tempValue+"px";
			}
			
		 },timespan);
		}
		var call = function(){
			alert(123);
		}
		document.getElementById("btn").onclick= function(){
				animateFun(document.getElementById("res"),'left',300,call);
		}
		document.getElementById("btn1").onclick= function(){
				animateFun(document.getElementById("res"),'left',0,call);
		}
	
	
	
	
}
</script>
</head>

<body>
<div id="res" style="width:100px; height:100px; background-color:#339966; position:absolute; top:0; left:0"></div>
<input type="button" style="margin-top:200px;" value="动起来"  id="btn"/>
<input type="button" style="margin-top:200px; margin-left:50px;" id="btn1" value="回去"/>
</body>
</html>
js 简单的缓动效果
<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>Music Trival</title>
		<script src="../jquery/jquery-1.7.1.js"></script>
        <script src="../jquery/myAnimation.js"></script>
		<script type="text/javascript">
			var resultbg = null;
			var resultImg = null;
			var okbtn = null;
			var isHideResult = false;

			function loadUI() {
				resultbg = document.getElementById("resultbg");
				resultImg = document.getElementById("resultImg");
				
				okbtn = document.getElementById("ok");
				okbtn.addEventListener("mousedown", onMouseDown);
				resultbg.addEventListener("mousedown", onMouseDown1);
			}

			function onMouseDown() {
				//Animation.moveDirection(resultbg,'top',200,20,Tween.Linear,callback);
				setResultBtnEffect("run");
			}
			function onMouseDown1() {
				//Animation.moveDirection("resultbg",'top',-200,50,Tween.Linear);
				setResultBtnEffect("close");
			}
			function callback(){
				}

			function setResultBtnEffect(str) {
				var setIntervalID = 0;
				var setTimeoutID = 0;
				var count = 0;
				if(str == "run") {
					isHideResult = true;
					count = -200;
					setIntervalID = window.setInterval(runShow, 50);
				} else if(str == "close") {
					isHideResult = false;
					count = 200;
					setIntervalID = window.setInterval(runHide, 50);
				}
				function runShow() {
					count += 50;
					resultbg.style.top = count + "px";
					if(count >= (200)) {
						resultbg.style.top = "200px";
						clearInterval(setIntervalID);
						setTimeoutID = setTimeout(runBack, 2000);
					}
				}

				function runBack() {
					clearTimeout(setTimeoutID);
					if(isHideResult) {
						setIntervalID = setInterval(runHide, 50);
						onNextQuestion("noclick");
						isHideResult = false;
					}
				}

				function runHide() {
					count -= 50;
					resultbg.style.top = count + "px";
					if(count <= (-200)) {
						resultbg.style.top = "-200px";
						clearInterval(setIntervalID);
					}
				}

			}
		</script>
		<style type="text/css" >
			.bg {
				background-color: #606;
				width: 320px;
				height: 480px;
			}
			.txt {
				position: relative;
				display: block;
				top: 130px;
				left: 70px;
				width: 180px;
			}
			.btn {
				position: relative;
				display: block;
				top: 350px;
				left: 95px;
				width: 120px;
			}
			.resultbg {
				position:absolute;
				display: block;
				z-index: 10;
				top: -200px;
				left: 80px;
			}
		</style>
	</head>
	<body onLoad="loadUI();">
		<div class="bg">
			<input  type="text" class="txt" id="name">
			<button   class="btn" id="ok">OK</button>
			    <article class="resultbg" id="resultbg">
				<button id="resultImg" style="width:150px; height:80px" >
					Right
				</button>
			</article>
		</div>
	</body>
</html>
Global site tag (gtag.js) - Google Analytics