<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>缓动算法示例</TITLE>
<META charset="gb2312">
</HEAD>
<style>
#DivExpress
{
width:400px;
height:50px;
border:1px solid green;
}
#Demo
{
position:relative;
border:1px solid red;
width:100px;
height:50px;
}
</style>
<BODY >
<center>
<div id="DivExpress">
<div id="Demo">
</div>
</div>
<select id="seleOption" οnchange="GetOptionValue(this)">
<option selected="true" value=-1 >
请选择缓动效果...
</option>
<option value=0 >
无缓动效果
</option>
<option value=1>
二次方的缓动
</option>
<option value=2>
三次方的缓动
</option>
<option value=3>
四次方的缓动
</option>
<option value=4>
五次方的缓动
</option>
<option value=5>
正弦曲线的缓动
</option>
<option value=6>
指数曲线的缓动
</option>
<option value=7>
圆形曲线的缓动
</option>
<option value=8>
指数衰减的正弦曲线缓动
</option>
<option value=9>
超过范围的三次方缓动
</option>
<option value=10>
指数衰减的反弹缓动
</option>
</select >
<select id="Show" disabled="true" οnchange="fun(this)">
<option selected="true" value="sele">
请选择...
</option>
<option value="easeIn">
easeIn
</option>
<option value="easeOut">
easeOut
</option>
<option value="easeInOut">
easeInOut
</option>
<select >
<input type="button" value="运行" οnclick="Run()">
</center>
</BODY>
</HTML>
<SCRIPT LANGUAGE="JavaScript">
<!--
//d duration(持续步长)。
//c change in value(变化增量);
//b beginning value(位置初始值);
//t current time(当前步长);
var b=0,c=300,d=100,t=0;
var obj=[];
var selectedObj=null;
var exeStr=null;
function fun(ob)
{
switch(ob.value)
{
case "easeIn":
//Tween.Quad.easeOut(t,b,c,d)
// confirm(obj.length)
exeStr="easeIn";
break;
case "easeOut":
exeStr="easeOut";
break;
case "easeInOut":
exeStr="easeInOut";
break;
}
}
function GetOptionValue(ob)
{
if(ob.value==-1||ob.value==0)
{
Show.disabled=true;
return null;
}
else
Show.disabled=false;
selectedObj=obj[ob.value];
}
function Run(){
var tmp;
//Demo.style.left = Tween.Quad.easeOut(t,b,c,d) + "px";
if(selectedObj!=null)
{
if(exeStr!=null)
{
switch(exeStr)
{
case "easeIn":
//d duration(持续步长)。
//c change in value(变化增量);
//b beginning value(位置初始值);
//t current time(当前步长);
tmp=selectedObj.easeIn(t,b,c,d);
break;
case "easeOut":
tmp=selectedObj.easeOut(t,b,c,d);
break;
case "easeInOut":
tmp=selectedObj.easeInOut(t,b,c,d);
break;
}
Demo.style.left=tmp+"px";
//alert(exeObj(t,b,c,d));
// Demo.style.left=exeObj(t,b,c,d);
window.status= selectedObj;
}else if(seleOption.value==0)
{
tmp=selectedObj.Linear(t,b,c,d);
}else
{
alert("请选择后续操作!");
return;
}
if(t<d)
{ t++; setTimeout(Run, 10); }
else
{
t=0;
}
}else
alert("请选择!");
}
//缓动
var Tween = {
//无缓动效果
Linear: function(t,b,c,d){ return c*t/d + b; },
//二次方的缓动
Quad: {
easeIn: function(t,b,c,d){
return c*(t/=d)*t + b;
},
easeOut: function(t,b,c,d){
return -c *(t/=d)*(t-2) + b;
},
easeInOut: function(t,b,c,d){
if ((t/=d/2) < 1) return c/2*t*t + b;
return -c/2 * ((--t)*(t-2) - 1) + b;
}
},
//三次方的缓动
Cubic: {
easeIn: function(t,b,c,d){
return c*(t/=d)*t*t + b;
},
easeOut: function(t,b,c,d){
return c*((t=t/d-1)*t*t + 1) + b;
},
easeInOut: function(t,b,c,d){
if ((t/=d/2) < 1) return c/2*t*t*t + b;
return c/2*((t-=2)*t*t + 2) + b;
}
},
//四次方的缓动
Quart: {
easeIn: function(t,b,c,d){
return c*(t/=d)*t*t*t + b;
},
easeOut: function(t,b,c,d){
return -c * ((t=t/d-1)*t*t*t - 1) + b;
},
easeInOut: function(t,b,c,d){
if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
return -c/2 * ((t-=2)*t*t*t - 2) + b;
}
},
//五次方的缓动
Quint: {
easeIn: function(t,b,c,d){
return c*(t/=d)*t*t*t*t + b;
},
easeOut: function(t,b,c,d){
return c*((t=t/d-1)*t*t*t*t + 1) + b;
},
easeInOut: function(t,b,c,d){
if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
return c/2*((t-=2)*t*t*t*t + 2) + b;
}
},
//正弦曲线的缓动
Sine: {
easeIn: function(t,b,c,d){
return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
},
easeOut: function(t,b,c,d){
return c * Math.sin(t/d * (Math.PI/2)) + b;
},
easeInOut: function(t,b,c,d){
return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}
},
//指数曲线的缓动
Expo: {
easeIn: function(t,b,c,d){
return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
},
easeOut: function(t,b,c,d){
return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
},
easeInOut: function(t,b,c,d){
if (t==0) return b;
if (t==d) return b+c;
if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
}
},
//圆形曲线的缓动
Circ: {
easeIn: function(t,b,c,d){
return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
},
easeOut: function(t,b,c,d){
return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
},
easeInOut: function(t,b,c,d){
if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
}
},
//指数衰减的正弦曲线缓动
Elastic: {
easeIn: function(t,b,c,d,a,p){
if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
else var s = p/(2*Math.PI) * Math.asin (c/a);
return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
},
easeOut: function(t,b,c,d,a,p){
if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
else var s = p/(2*Math.PI) * Math.asin (c/a);
return (a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b);
},
easeInOut: function(t,b,c,d,a,p){
if (t==0) return b; if ((t/=d/2)==2) return b+c; if (!p) p=d*(.3*1.5);
if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
else var s = p/(2*Math.PI) * Math.asin (c/a);
if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
}
},
//超过范围的三次方缓动
Back: {
easeIn: function(t,b,c,d,s){
if (s == undefined) s = 1.70158;
return c*(t/=d)*t*((s+1)*t - s) + b;
},
easeOut: function(t,b,c,d,s){
if (s == undefined) s = 1.70158;
return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
},
easeInOut: function(t,b,c,d,s){
if (s == undefined) s = 1.70158;
if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
}
},
//指数衰减的反弹缓动
Bounce: {
easeIn: function(t,b,c,d){
return c - Tween.Bounce.easeOut(d-t, 0, c, d) + b;
},
easeOut: function(t,b,c,d){
if ((t/=d) < (1/2.75)) {
return c*(7.5625*t*t) + b;
} else if (t < (2/2.75)) {
return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
} else if (t < (2.5/2.75)) {
return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
} else {
return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
}
},
easeInOut: function(t,b,c,d){
if (t < d/2) return Tween.Bounce.easeIn(t*2, 0, c, d) * .5 + b;
else return Tween.Bounce.easeOut(t*2-d, 0, c, d) * .5 + c*.5 + b;
}
}
}
//Run();
obj[obj.length]= Tween.Linear;
obj[obj.length]= Tween.Quad;
obj[obj.length]= Tween.Cubic;
obj[obj.length]= Tween.Quart;
obj[obj.length]= Tween.Quint;
obj[obj.length]= Tween.Sine;
obj[obj.length]= Tween.Expo;
obj[obj.length]= Tween.Circ;
obj[obj.length]= Tween.Elastic;
obj[obj.length]= Tween.Back;
obj[obj.length]= Tween.Bounce;
//-->
</SCRIPT>