JavaScript運動框架之速度時間版本

分類:技術 時間:2017-01-13

運動,其實就是在一段時間內改變 leftrightwidthheightopactiy 的值,到達目的地之后停止

  • 位移 top , left
  • 折疊 width , height
  • 淡入淡出 opacity
  • 時間有關系
    • setInterval
    • setTimeout
  • 用javascript直接獲取行間樣式很容易,但如果要獲取非行間樣式那我們只能借助函數了。我這里編寫了一個名為getStyle的函數,專門處理取非行間的樣式
functiongetStyle(obj,attr){
	return getComputedStyle(obj)[attr]?getComputedStyle(obj)[attr]:obj.currentStyle[attr];
}

1.2 一些案例演示

1.2.1 運動之速

lt;divid="box"gt;lt;/divgt;
#box {
	width: 100px;
	height: 100px;
	background: red;
	 position: relative;
	left: 0;
}
var box = document.getElementById("box");
var speed = 0; //步長
var target = 600;
var timer = null;
timer = setInterval(function(){
	var curr = parseInt(getStyle(box,"left")); //去除getStyle(box,"left")的單位
	if(curr == target){
		clearInterval(timer);
		speed = 0;
		alert("運動結束");
	}else{
		speed  =10;
		box.style.left = speed   "px";
	}
	

},1000/30);

//監控left的值的變化 怎么樣拿到left的值
//alert(getComputedStyle(box)["width"]);
//alert(box.currentStyle["left"]);
// currentStyle --IE 
// getComputedStyle -- 非IE

functiongetStyle(obj,attr){
	return getComputedStyle(obj)[attr]?getComputedStyle(obj)[attr]:obj.currentStyle[attr];
}

在線演示

1.2.2 運動速度之封裝1

lt;divid="ball"gt;lt;/divgt;
#ball {
	width: 100px;
	height: 100px;
	background: blue;
}
var ball = document.getElementById("ball");

ball.onmouseover = function(){
	//同時變換 用的最多
	//move(this,"width",500,10);
	//move(this,"height",500,10);
	move(ball,{"width":400,"height":300},10);
}
ball.onmouseout = function(){
	//move(this,"width",100,-10);
	//move(this,"height",100,-10);
	move(ball,{"width":100,"height":100},-10);
}
functionmove(obj,json,speed){
	clearInterval(obj.timer);
	var mark = true;
	obj.timer = setInterval(function(){
		for(var attr in json){
			var curr = parseInt(getStyle(obj,attr));
			var target = json[attr];
			if(curr != target){
				obj.style[attr] = curr speed "px";
				mark = false;
		  }
		}
		if(mark){
			clearInterval(obj.timer);
		}
	},1000/30);
}


functiongetStyle(obj,attr){
	return getComputedStyle(obj)[attr]?getComputedStyle(obj)[attr]:obj.currentStyle[attr];
}
  • 需要注意的地方
    • 當需要兩個動畫的時候,會執行后面一個,解決辦法如下,回調函數
    • 當需要兩個以上的時候,需要考慮是否可寫一行代碼變換多個屬性
    • 變換不一致的時候,定時器被提前清除

在線演示

1.2.3 運動速度之封裝2–增加opacity

lt;divid="ball"gt;lt;/divgt;
#ball {
  position: relative;
  left: 0;
  top: 0;
  width: 100px;
  height: 100px;
  background: blue;
  opacity: 1;
}
var ball = document.getElementById("ball");
ball.onmouseover = function(){
	move(ball,{"width":300,"height":300,"opacity":0.3});
}
// ball.onmouseout = function(){
// move(ball,{"width":100,"height":100},-10);
// }
functionmove(obj,json){
	clearInterval(obj.timer);
	var mark = true;
	obj.timer = setInterval(function(){
		for(var attr in json){
			var curr = null;
			var target = json[attr];
			var speed = null;
			if(attr == "opacity"){
				curr = getStyle(obj,attr)*100;
				speed = (target*100-curr)*0.15;
			}else {
				curr = parseInt(getStyle(obj,attr));
				speed = (target - curr)*0.15;
			}
			speed = speedgt;0 ? Math.ceil(speed):Math.floor(speed);
			if(curr != target){
				if(attr == "opacity"){
					obj.style[attr] = (curr speed)/100;
				}else {
					obj.style[attr] = curr speed "px";
				}
				
				mark = false;
		  }
		}
		if(mark){
			clearInterval(obj.timer);
		}
	},1000/30);
}


functiongetStyle(obj,attr){
	return getComputedStyle(obj)[attr]?getComputedStyle(obj)[attr]:obj.currentStyle[attr];
}

在線演示

  • 需要注意的地方
    • 當需要兩個動畫的時候,會執行后面一個,解決辦法如下,回調函數
    • 當需要兩個以上的時候,需要考慮是否可寫一行代碼變換多個屬性
    • 變換不一致的時候,定時器被提前清除
    • 速度 speed 不要寫死

1.3 運動框架之應用

1.3.1 分享按鈕

lt;divid="ball"gt;lt;/divgt;
lt;divid="box1"gt;
  lt;divid="box2"gt;分享到lt;/divgt;
lt;/divgt;
var box1 = document.getElementById("box1");
var ball = document.getElementById("ball");

box1.onmouseover = function(){
  move(this,"left",0,10);
}
box1.onmouseout = function(){
  move(this,"left",-100,-10);
}
//問題一:當需要兩個動畫的時候,會執行后面一個,解決辦法如下,回調函數
ball.onmouseover = function(){
  //同時變換 用的最多
  //move(this,"width",500,10);
  //move(this,"height",500,10);
  //列隊在執行
  move(ball,"width",500,10,function(){
    move(ball,"height",500,10);
  });
}
ball.onmouseout = function(){
  //move(this,"width",100,-10);
  //move(this,"height",100,-10);
  move(ball,"width",100,-10,function(){
    move(ball,"height",100,-10);
  });
}
var timer = null;
functionmove(obj,attr,target,speed,callback){
  clearInterval(timer); //obj.timer緩存到各自的obj下
  timer = setInterval(function(){
    var curr = parseInt(getStyle(obj,attr));
    if(curr == target){
      clearInterval(timer);
      callback  callback();
    }else {
      obj.style[attr] = curr speed "px";
    }
  },1000/30);
}




functiongetStyle(obj,attr){
  return getComputedStyle(obj)[attr]?getComputedStyle(obj)[attr]:obj.currentStyle[attr];
}

在線演示

1.3.2運動框架之輪播圖應用

1.3.2.1 焦點輪播–左右-無縫-速度版實現

lt;divid="box"gt;
	lt;ulid="imgBox"gt;
		lt;ligt;![](http://upload-images.jianshu.io/upload_images/1480597-c72819402fb928e8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)lt;/ligt;
		lt;ligt;![](http://upload-images.jianshu.io/upload_images/1480597-6830ca74fe1e6fcd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)lt;/ligt;
		lt;ligt;![](http://upload-images.jianshu.io/upload_images/1480597-5d38376e63ffd0b0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)lt;/ligt;
		lt;ligt;![](http://upload-images.jianshu.io/upload_images/1480597-2aa932ffbba4091e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)lt;/ligt;
		lt;ligt;![](http://upload-images.jianshu.io/upload_images/1480597-c72819402fb928e8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)lt;/ligt;
	lt;/ulgt;
	lt;olid="btn"gt;
		lt;liclass="active"gt;1lt;/ligt;
		lt;ligt;2lt;/ligt;
		lt;ligt;3lt;/ligt;
		lt;ligt;4lt;/ligt;
	lt;/olgt;
lt;/divgt;
lt;scriptsrc="http://7xq6al.com1.z0.glb.clouddn.com/Animate.min.js"gt;lt;/scriptgt;
lt;scripttype="text/javascript"gt;
    var box = document.querySelector("#box");
	var imgUl = document.querySelector("#imgBox");
	var btns = document.querySelector("#btn").querySelectorAll("li");
	var len = btns.length;
	var lenImg = imgUl.querySelectorAll("li").length;
	var index = 0; //控制img的索引
	var cindex = 0;//控制按鈕的索引
	var timer = null;
	
	for (var i=0;ilt;len;i  ) {
			(function(index){
				btns[index].onmouseover = function(){
					for (var j=0;jlt;len;j  ){
						btns[j].className = "";
				}
					cindex = index;//保持索引同步
					animateSpeed(imgUl,{"left":-970*index});
					this.className = "active";
				}
			})(i);
	}
	functionautoPlay(){
		index  ;
		cindex  ;
		cindex %=len;//限制長度
		for (var j=0;jlt;len;j  ){
			btns[j].className = "";
		}
		animateSpeed(imgUl,{"left":-970*index},function(){
			
			if(index == lenImg-1){
				this.style.left = 0;
				index = 0;
			}
		});
		btns[cindex].className = "active";
	}
	timer = setInterval(autoPlay,2000);
	box.onmouseover = function(){
		clearInterval(timer);
	}
	box.onmouseout = function(){
		timer = setInterval(autoPlay,2000);
	}
lt;/scriptgt;
*{
  padding: 0;
  margin: 0;
}
body{
  font-size: 14px;
  font-family: "微軟雅黑";
}
ul,li{
  list-style: none;
}
#box {
  position: relative;
  width: 970px;
  height: 350px;
  margin: 30px auto;
  overflow: hidden;
}
#imgBox {
  width:1000%;/*自動計算百分比*/
  position: absolute;
  left: 0;
}
#imgBox li{
  width: 970px;
  height: 350px;
  float: left;
}

#imgBox li img {
  width: 970px;
  height: 350px;
}
#btn {
  width: 120px;
  position: absolute;
  right: 10px;
  bottom: 10px;
}
#btn li {
  width: 20px;
  height: 20px;
  line-height: 20px;
  border-radius: 50%;
  text-align: center;
  cursor: pointer;
  background: #fff;
  margin: 0 2px;
  float: left;
}
#btn li.active {
  background: #F17A5C;
  color: #fff;
}
//速度版本
(function(win){
    functionmove(obj,json,callback){
        clearInterval(obj.timer);
        obj.timer = setInterval(function(){
            var mark = true;
            for(var attr in json){
                var cur = null;
                if(attr == "opacity"){
                    cur = getStyle(obj,attr)*100;
                }else{
                    //如果沒寫 默認填充成0
                    cur = parseInt(getStyle(obj,attr))||0;
                }
                var target = json[attr];
                var speed = (target - cur)*0.2;
                speed = speedgt;0?Math.ceil(speed):Math.floor(speed);
                if(cur != target){
                    if(attr == "opacity"){
                        //IE opacity兼容問題
                        obj.style.filter = "alpha(opacity=" (cur speed) ")";
                        obj.style[attr] = (cur   speed)/100;
                    }else{
                        obj.style[attr] = cur   speed   "px";
                    }
                    mark = false;

                };
            }
            if(mark){
                clearInterval(obj.timer);
                callback  callback.call(obj);
            }
        },1000/30);
    }
    win.animateSpeed = move;
})(window);

 	
functiongetStyle(obj,attr){
	return getComputedStyle(obj)[attr]?getComputedStyle(obj)[attr]:obj.currentStyle[attr];
}

functiongetId(id){
	return document.getElementById(id);
}

二、JavaScript運動框架之時間版

2.1 關于運動

  • 速度的運動 通過速度來控制元素的 位移 / 折疊 / 淡入淡出
  • 時間的運動 通過時間來控制元素的 位移 / 折疊 / 淡入淡出(jQuery)
  • 時間的運動 基于一些數學公式 勻速運動 在路程的每一個點 速度都一樣

2.2 一些案例演示

2.3 運動框架之時間版本-借助animate一些函數實現–綜合完整版

lt;divid="box2"gt;lt;/divgt;
lt;divid="box3"gt;lt;/divgt;
lt;divid="box4"gt;lt;/divgt;
lt;divid="box5"gt;lt;/divgt;
lt;scripttype="text/javascript"gt;

 //時間版本
 getId("box2").onclick = function(){
 	animateTime(getId("box2"),{
 		"left":500,
 		"opacity":100
 	},1000,"elasticOut",function(){
 		this.innerHTML = "我是時間版本";
 	});
 }
getId("box3").onclick = function(){
 	animateTime(getId("box3"),{
 		"left":500,
 		"opacity":100
 	},1000,"backIn",function(){
 		this.innerHTML = "我是時間版本";
 	});
 }
getId("box4").onclick = function(){
 	animateTime(getId("box4"),{
 		"left":500,
 		"opacity":100
 	},1000,"bounceIn",function(){
 		this.innerHTML = "我是時間版本";
 	});
 }
getId("box5").onclick = function(){
 	animateTime(getId("box5"),{
 		"left":500,
 		"opacity":100
 	},1000,"bounceBoth",function(){
 		this.innerHTML = "我是時間版本";
 	});
 }

 
 
lt;/scriptgt;
#box1,#box2,#box3,#box4,#box5 {
	position: relative;
	width: 100px;
	height: 100px;
	line-height: 100px;
	text-align: center;
	background: red;
	color: #fff;
	font-size: 12px;
	opacity: 0.5;
	filter:alpha(opcity=20);/**兼容IE*/
	margin: 10px;
	
}
/*t b c d
t current time   :nTime-sTime
b begining time  :curr
c chang in value :變化量end-curr
d duration       :持續時間 time */
/**
* 
* @param {Object} obj 元素對象
* @param {Object} json 多個屬性
* @param {Object} time 變化時間
* @param {Object} prop 運動函數
* @param {Object} callback 回調函數
*/
//時間版本
(function(win){ 
functionmove(obj,json,time,prop,callback){
//一般定時器結束后最好清除
clearInterval(obj.timer);
var curr = {};
var end = {};
//通過for in 在上車前把所有東西裝到包里
for(var attr in json){
	if(attr == "opacity"){//opacity特殊東西特殊對待
		curr[attr] = getStyle(obj,attr)*100;//化為整數好計算
	}else{
		curr[attr] = parseInt(getStyle(obj,attr))||0;
	}
	end[attr] = json[attr];
	
}


//如果沒寫默認值 默認就是0 不然在IE出問題
//var curr = parseInt(getStyle(obj,attr))||0;
//var end = target;
var sTime = new Date();//開始時間T0
//開始變換了
obj.timer = setInterval(function(){
	var nTime = new Date();//當前時間Tt
	var t = nTime -sTime;
	var d = time;
	//St = (Tt-T0)/Time*(S-S0) S0
	//(nTime-sTime)/time 比例最多為1
	/*var prop = (nTime-sTime)/time; */
	if(t gt;=d){
		t = d;
		clearInterval(obj.timer);
		callback  callback.call(obj);
	}
	for(var attr in json){
		var b = curr[attr];
		var c = end[attr] - b;
		if(attr == "opacity"){
			//var s = prop*(end[attr]-curr[attr]) curr[attr];
			var s = Tween[prop](t,b,c,d);
			obj.style[attr] = s/100;
			obj.style.filter = "alpha(opacity=" s ")";
		}else{
			//var s = prop*(end[attr]-curr[attr]) curr[attr];
			var s = Tween[prop](t,b,c,d);
			obj.style[attr] = s "px";
		}

	}

	
},13);
var Tween = {
    linear: function(t, b, c, d){  //勻速
        return c*t/d   b;   // t/d = prop;
    },
    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;
    },
    easeBoth: function(t, b, c, d){  //加速減速曲線
        if ((t/=d/2) lt; 1) {
            return c/2*t*t   b;
        }
        return -c/2 * ((--t)*(t-2) - 1)   b;
    },
    easeInStrong: function(t, b, c, d){  //加加速曲線
        return c*(t/=d)*t*t*t   b;
    },
    easeOutStrong: function(t, b, c, d){  //減減速曲線
        return -c * ((t=t/d-1)*t*t*t - 1)   b;
    },
    easeBothStrong: function(t, b, c, d){  //加加速減減速曲線
        if ((t/=d/2) lt; 1) {
            return c/2*t*t*t*t   b;
        }
        return -c/2 * ((t-=2)*t*t*t - 2)   b;
    },
    elasticIn: function(t, b, c, d, a, p){  //正弦衰減曲線(彈動漸入)
        if (t === 0) {
            return b;
        }
        if ( (t /= d) == 1 ) {
            return b c;
        }
        if (!p) {
            p=d*0.3;
        }
        if (!a || a lt; 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;
    },
    elasticOut: function(t, b, c, d, a, p){    //正弦增強曲線(彈動漸出)
        if (t === 0) {
            return b;
        }
        if ( (t /= d) == 1 ) {
            return b c;
        }
        if (!p) {
            p=d*0.3;
        }
        if (!a || a lt; 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;
    },
    elasticBoth: function(t, b, c, d, a, p){
        if (t === 0) {
            return b;
        }
        if ( (t /= d/2) == 2 ) {
            return b c;
        }
        if (!p) {
            p = d*(0.3*1.5);
        }
        if ( !a || a lt; Math.abs(c) ) {
            a = c;
            var s = p/4;
        }
        else {
            var s = p/(2*Math.PI) * Math.asin (c/a);
        }
        if (t lt; 1) {
            return - 0.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 )*0.5   c   b;
    },
    backIn: function(t, b, c, d, s){     //回退加速(回退漸入)
        if (typeof s == 'undefined') {
           s = 1.70158;
        }
        return c*(t/=d)*t*((s 1)*t - s)   b;
    },
    backOut: function(t, b, c, d, s){
        if (typeof s == 'undefined') {
            s = 3.70158;  //回縮的距離
        }
        return c*((t=t/d-1)*t*((s 1)*t   s)   1)   b;
    },
    backBoth: function(t, b, c, d, s){
        if (typeof s == 'undefined') {
            s = 1.70158;
        }
        if ((t /= d/2 ) lt; 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;
    },
    bounceIn: function(t, b, c, d){    //彈球減振(彈球漸出)
        return c - Tween['bounceOut'](d-t, 0, c, d)   b;
    },
    bounceOut: function(t, b, c, d){
        if ((t/=d) lt; (1/2.75)) {
            return c*(7.5625*t*t)   b;
        } else if (t lt; (2/2.75)) {
            return c*(7.5625*(t-=(1.5/2.75))*t   0.75)   b;
        } else if (t lt; (2.5/2.75)) {
            return c*(7.5625*(t-=(2.25/2.75))*t   0.9375)   b;
        }
        return c*(7.5625*(t-=(2.625/2.75))*t   0.984375)   b;
    },
    bounceBoth: function(t, b, c, d){
        if (t lt; d/2) {
            return Tween['bounceIn'](t*2, 0, c, d) * 0.5   b;
        }
        return Tween['bounceOut'](t*2-d, 0, c, d) * 0.5   c*0.5   b;
        }
};
}
win.animateTime = move;	
})(window);

在線演示

三、運動框架之時間速度版完整封裝

/*t b c d
t current time   :nTime-sTime
b begining time  :curr
c chang in value :變化量end-curr
d duration       :持續時間 time */
/**
 * 
 * @param {Object} obj 元素對象
 * @param {Object} json 多個屬性
 * @param {Object} time 變化時間
 * @param {Object} prop 運動函數
 * @param {Object} callback 回調函數
 */
//時間版本
(function(win){ 
	functionmove(obj,json,time,prop,callback){
	//一般定時器結束后最好清除
	clearInterval(obj.timer);
	var curr = {};
	var end = {};
	//通過for in 在上車前把所有東西裝到包里
	for(var attr in json){
		if(attr == "opacity"){//opacity特殊東西特殊對待
			curr[attr] = getStyle(obj,attr)*100;//化為整數好計算
		}else{
			curr[attr] = parseInt(getStyle(obj,attr))||0;
		}
		end[attr] = json[attr];
		
	}
	
	
	//如果沒寫默認值 默認就是0 不然在IE出問題
	//var curr = parseInt(getStyle(obj,attr))||0;
	//var end = target;
	var sTime = new Date();//開始時間T0
	//開始變換了
	obj.timer = setInterval(function(){
		var nTime = new Date();//當前時間Tt
		var t = nTime -sTime;
		var d = time;
		//St = (Tt-T0)/Time*(S-S0) S0
		//(nTime-sTime)/time 比例最多為1
		/*var prop = (nTime-sTime)/time; */
		if(t gt;=d){
			t = d;
			clearInterval(obj.timer);
			callback  callback.call(obj);
		}
		for(var attr in json){
			var b = curr[attr];
			var c = end[attr] - b;
			if(attr == "opacity"){
				//var s = prop*(end[attr]-curr[attr]) curr[attr];
				var s = Tween[prop](t,b,c,d);
				obj.style[attr] = s/100;
				obj.style.filter = "alpha(opacity=" s ")";
			}else{
				//var s = prop*(end[attr]-curr[attr]) curr[attr];
				var s = Tween[prop](t,b,c,d);
				obj.style[attr] = s "px";
			}

		}

		
	},13);
	var Tween = {
        linear: function(t, b, c, d){  //勻速
            return c*t/d   b;   // t/d = prop;
        },
        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;
        },
        easeBoth: function(t, b, c, d){  //加速減速曲線
            if ((t/=d/2) lt; 1) {
                return c/2*t*t   b;
            }
            return -c/2 * ((--t)*(t-2) - 1)   b;
        },
        easeInStrong: function(t, b, c, d){  //加加速曲線
            return c*(t/=d)*t*t*t   b;
        },
        easeOutStrong: function(t, b, c, d){  //減減速曲線
            return -c * ((t=t/d-1)*t*t*t - 1)   b;
        },
        easeBothStrong: function(t, b, c, d){  //加加速減減速曲線
            if ((t/=d/2) lt; 1) {
                return c/2*t*t*t*t   b;
            }
            return -c/2 * ((t-=2)*t*t*t - 2)   b;
        },
        elasticIn: function(t, b, c, d, a, p){  //正弦衰減曲線(彈動漸入)
            if (t === 0) {
                return b;
            }
            if ( (t /= d) == 1 ) {
                return b c;
            }
            if (!p) {
                p=d*0.3;
            }
            if (!a || a lt; 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;
        },
        elasticOut: function(t, b, c, d, a, p){    //正弦增強曲線(彈動漸出)
            if (t === 0) {
                return b;
            }
            if ( (t /= d) == 1 ) {
                return b c;
            }
            if (!p) {
                p=d*0.3;
            }
            if (!a || a lt; 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;
        },
        elasticBoth: function(t, b, c, d, a, p){
            if (t === 0) {
                return b;
            }
            if ( (t /= d/2) == 2 ) {
                return b c;
            }
            if (!p) {
                p = d*(0.3*1.5);
            }
            if ( !a || a lt; Math.abs(c) ) {
                a = c;
                var s = p/4;
            }
            else {
                var s = p/(2*Math.PI) * Math.asin (c/a);
            }
            if (t lt; 1) {
                return - 0.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 )*0.5   c   b;
        },
        backIn: function(t, b, c, d, s){     //回退加速(回退漸入)
            if (typeof s == 'undefined') {
               s = 1.70158;
            }
            return c*(t/=d)*t*((s 1)*t - s)   b;
        },
        backOut: function(t, b, c, d, s){
            if (typeof s == 'undefined') {
                s = 3.70158;  //回縮的距離
            }
            return c*((t=t/d-1)*t*((s 1)*t   s)   1)   b;
        },
        backBoth: function(t, b, c, d, s){
            if (typeof s == 'undefined') {
                s = 1.70158;
            }
            if ((t /= d/2 ) lt; 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;
        },
        bounceIn: function(t, b, c, d){    //彈球減振(彈球漸出)
            return c - Tween['bounceOut'](d-t, 0, c, d)   b;
        },
        bounceOut: function(t, b, c, d){
            if ((t/=d) lt; (1/2.75)) {
                return c*(7.5625*t*t)   b;
            } else if (t lt; (2/2.75)) {
                return c*(7.5625*(t-=(1.5/2.75))*t   0.75)   b;
            } else if (t lt; (2.5/2.75)) {
                return c*(7.5625*(t-=(2.25/2.75))*t   0.9375)   b;
            }
            return c*(7.5625*(t-=(2.625/2.75))*t   0.984375)   b;
        },
        bounceBoth: function(t, b, c, d){
            if (t lt; d/2) {
                return Tween['bounceIn'](t*2, 0, c, d) * 0.5   b;
            }
            return Tween['bounceOut'](t*2-d, 0, c, d) * 0.5   c*0.5   b;
            }
    };
 }
	win.animateTime = move;	
 })(window);


//速度版本
(function(win){
    functionmove(obj,json,callback){
        clearInterval(obj.timer);
        obj.timer = setInterval(function(){
            var mark = true;
            for(var attr in json){
                var cur = null;
                if(attr == "opacity"){
                    cur = getStyle(obj,attr)*100;
                }else{
                    //如果沒寫 默認填充成0
                    cur = parseInt(getStyle(obj,attr))||0;
                }
                var target = json[attr];
                var speed = (target - cur)*0.2;
                speed = speedgt;0?Math.ceil(speed):Math.floor(speed);
                if(cur != target){
                    if(attr == "opacity"){
                        //IE opacity兼容問題
                        obj.style.filter = "alpha(opacity=" (cur speed) ")";
                        obj.style[attr] = (cur   speed)/100;
                    }else{
                        obj.style[attr] = cur   speed   "px";
                    }
                    mark = false;

                };
            }
            if(mark){
                clearInterval(obj.timer);
                callback  callback.call(obj);
            }
        },1000/30);
    }
    win.animateSpeed = move;
})(window);

 	
functiongetStyle(obj,attr){
	return getComputedStyle(obj)[attr]?getComputedStyle(obj)[attr]:obj.currentStyle[attr];
}

functiongetId(id){
	return document.getElementById(id);
}

Tags: JavaScript

文章來源:http://blog.poetries.top/2017/01/12/js-animate/


ads
ads

相關文章
ads

相關文章

ad