关闭

运动框架

55人阅读 评论(0) 收藏 举报
/**
 * Created by Administrator on 2016/6/28.
 */

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;
        }
    }
};

上面是Tween.js。下面分别为需链接它的两个网页

06.tween动画演示

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<style type="text/css">
		#box{
			width: 200px;
			height: 100px;
			background: pink;
		}
	</style>
	<body>
		选择动画类型:<select id="type">
			
			<option >Linear</option>
			<option >Quad</option>
			<option >Bounce</option>
			<option >Back</option>
			<option >Quart</option>
			<option >Quint</option>
			<option >Sine</option>
			<option >Expo</option>
			<option >Elastic</option>
		</select>
		选择运动方式:<select id="method">
			
			<option >easeIn</option>
			<option >easeOut</option>
			<option >easeInOut</option>
		</select>
		<button id="btn">演示</button>
		<div id="box"></div>
	</body>
	<script src="Tween.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		//1.获取元素
		
		var type = document.getElementById('type');
		
		var method = document.getElementById('method');
		
		var btn = document.getElementById('btn');
		
		var box = document.getElementById('box');
		
		//2.添加单击事件
		
		btn.onclick = function(){
			
			play();
			
		}
		
		//3.自定义动画函数
		
		var startTime = 0;
		
		var endTime = 200;
		
		var startWidth = 0;
		
		var endWidth = 600;
		
		
		
		function play(){
			
			//4.每次让时间进行递增
			
			startTime++;
			
			//5.如果开始时间大于结束时间return
			
			if(startTime>endTime){
				
				//5.1动画完成之后把四个变量再重置  把div的样式重置
				
				startTime = 0;
				
				box.style.width = '200px';
				
				return;
				
			}
			//6.根据用户表单选择的type类型执行tween里面的动画类型,然后再根据用户表单选择的动画方式设置对应的运动方式
			box.style.width = Tween[type.value][method.value](startTime,startWidth,endWidth,endTime)+'px';
			
			
			setTimeout(play,10);
			
		}
		
	</script>
</html>
05..tween.js动画的使用

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<style type="text/css">
		#box,#box1{
			width: 200px;
			height: 150px;
			margin: 50px;
			background: pink;
		}
	</style>
	<body>
		<div id="box"></div>
		<div id="box1"></div>
		<button id="btn">开启动画</button>
	</body>
	<script src="Tween.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		//1.获取元素
		
		var box = document.getElementById('box');
		var box1 = document.getElementById('box1');
		
		var btn = document.getElementById('btn');
		
		/**
		 * t 开始时间   0
		 * b 开始值     假如当前宽200px
		 * c 结束值    结束之后宽300px
		 * d 结束时间   50
		 * 
		 * easeIn 加速 先慢后快
		 * eseOut 减速 先快后慢
		 * easeInOut 先慢 后快 再慢   加速  减速
		 */
		//2.声明四个变量保存这些值
		
		var startTime = 0;
		
		var startWidth = 0;
		
		var endWidth = 800;
		
		var endTime = 200;
		
		//3.自定义函数
		
		function play(){
			//4.每次递归执行这个函数让时间+1
			
			console.log(startTime);
			startTime++;
			//4.1判断这个开始时间是否大于等于结束时间 return
			if(startTime>endTime){
				
				return;
			}
			
			box.style.width = Tween.Back.easeInOut(startTime,startWidth,endWidth,endTime)+'px';
			box1.style.width = Tween.Quad.easeIn(startTime,startWidth,endWidth,endTime)+'px';
			
			setTimeout(play,10);
		}
		
		//5.点击执行play
		
		btn.onclick = function(){
			
			play();
		}
		
		
		
		
	</script>
</html>



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5753次
    • 积分:484
    • 等级:
    • 排名:千里之外
    • 原创:44篇
    • 转载:5篇
    • 译文:0篇
    • 评论:3条
    文章分类
    文章存档