Created
August 27, 2014 01:19
-
-
Save 303182519/7be593ad3a6faa3bbb61 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var yyAnimate={ | |
//缓冲运动 | |
buffer:function(obj, cur, target, fnDo, fnEnd, fs){ | |
if(!fs)fs=6; | |
var now={}; | |
var x=0; | |
var v=0; | |
if(!obj.__last_timer)obj.__last_timer=0; | |
var t=new Date().getTime(); | |
if(t-obj.__last_timer>20) | |
{ | |
fnMove(); | |
obj.__last_timer=t; | |
} | |
clearInterval(obj.timer); | |
obj.timer=setInterval(fnMove, 20); | |
function fnMove(){ | |
v=Math.ceil((100-x)/fs); | |
x+=v; | |
for(var i in cur) | |
{ | |
now[i]=(target[i]-cur[i])*x/100+cur[i]; | |
} | |
if(fnDo)fnDo.call(obj, now); | |
if(Math.abs(v)<1 && Math.abs(100-x)<1) | |
{ | |
clearInterval(obj.timer); | |
if(fnEnd)fnEnd.call(obj, target); | |
} | |
} | |
}, | |
//匀速运动 | |
linear:function(obj, cur, target, fnDo, fnEnd, fs){ | |
if(!fs)fs=50; | |
var now={}; | |
var x=0; | |
var v=0; | |
if(!obj.__last_timer)obj.__last_timer=0; | |
var t=new Date().getTime(); | |
if(t-obj.__last_timer>20) | |
{ | |
fnMove(); | |
obj.__last_timer=t; | |
} | |
clearInterval(obj.timer); | |
obj.timer=setInterval(fnMove, 20); | |
v=100/fs; | |
function fnMove(){ | |
x+=v; | |
for(var i in cur) | |
{ | |
now[i]=(target[i]-cur[i])*x/100+cur[i]; | |
} | |
if(fnDo)fnDo.call(obj, now); | |
if(Math.abs(100-x)<1) | |
{ | |
clearInterval(obj.timer); | |
if(fnEnd)fnEnd.call(obj, target); | |
} | |
} | |
}, | |
//弹性运动 | |
flex:function(obj, cur, target, fnDo, fnEnd, fs){ | |
if(!fs)fs=50; | |
var now={}; | |
var x=0; | |
var v=0; | |
if(!obj.__last_timer)obj.__last_timer=0; | |
var t=new Date().getTime(); | |
if(t-obj.__last_timer>20) | |
{ | |
fnMove(); | |
obj.__last_timer=t; | |
} | |
clearInterval(obj.timer); | |
obj.timer=setInterval(fnMove, 20); | |
v=100/fs; | |
function fnMove(){ | |
x+=v; | |
for(var i in cur) | |
{ | |
now[i]=(target[i]-cur[i])*x/100+cur[i]; | |
} | |
if(fnDo)fnDo.call(obj, now); | |
if(Math.abs(100-x)<1) | |
{ | |
clearInterval(obj.timer); | |
if(fnEnd)fnEnd.call(obj, target); | |
} | |
} | |
}, | |
//摆动运动 | |
swing:function(obj, cur, target, fnDo, fnEnd, acc){ | |
if(!acc)acc=0.1; | |
var now={}; | |
var x=0; //0-100 | |
if(!obj.__swing_v)obj.__swing_v=0; | |
if(!obj.__last_timer)obj.__last_timer=0; | |
var t=new Date().getTime(); | |
if(t-obj.__last_timer>20) | |
{ | |
fnMove(); | |
obj.__last_timer=t; | |
} | |
clearInterval(obj.timer); | |
obj.timer=setInterval(fnMove, 20); | |
function fnMove(){ | |
if(x<50) | |
{ | |
obj.__swing_v+=acc; | |
} | |
else | |
{ | |
obj.__swing_v-=acc; | |
} | |
//if(Math.abs(obj.__flex_v)>MAX_SPEED)obj.__flex_v=obj.__flex_v>0?MAX_SPEED:-MAX_SPEED; | |
x+=obj.__swing_v; | |
//alert(x+','+obj.__swing_v); | |
for(var i in cur) | |
{ | |
now[i]=(target[i]-cur[i])*x/100+cur[i]; | |
} | |
if(fnDo)fnDo.call(obj, now); | |
if(/*Math.abs(obj.__swing_v)<1 || */Math.abs(100-x)<1) | |
{ | |
clearInterval(obj.timer); | |
if(fnEnd)fnEnd.call(obj, target); | |
obj.__swing_v=0; | |
} | |
} | |
}, | |
//设置css | |
setCss:function(obj,oAttr,val){ | |
var sAttr=""; | |
var arr=["Webkit","Moz","O","ms",""]; | |
if(val){ | |
var oBj={}; | |
oBj[oAttr]=val; | |
oAttr=oBj; | |
} | |
for(sAttr in oAttr) | |
if(sAttr.charAt(0)=="$"){ | |
for(var i=0;i<arr.length;i++){ | |
obj.style[arr[i]+sAttr.substring(1)]=oAttr[sAttr]; | |
} | |
}else{ | |
var value=oAttr[sAttr]; | |
switch(sAttr){ | |
case 'width': | |
case 'height': | |
case 'paddingLeft': | |
case 'paddingTop': | |
case 'paddingRight': | |
case 'paddingBottom': | |
value=Math.max(value,0); | |
case 'left': | |
case 'top': | |
case 'marginLeft': | |
case 'marginTop': | |
case 'marginRight': | |
case 'marginBottom': | |
if(typeof value=="string"){ | |
obj.style[sAttr]=value; | |
}else{ | |
obj.style[sAttr]=value + 'px'; | |
} | |
break; | |
case 'opacity': | |
if(value<0){ | |
value=0; | |
} | |
obj.style.filter="alpha(opacity:"+value+")"; | |
obj.style.opacity=value/100; | |
break; | |
default: | |
obj.style[sAttr]=value; | |
} | |
} | |
}, | |
//预加载图片 | |
preloadImgs:function(arr, fnSucc, fnFaild, fnProgress){ | |
var loaded=0; | |
for(var i=0;i<arr.length;i++){ | |
var oImg=new Image(); | |
oImg.onload=function (){ | |
loaded++; | |
fnProgress&&fnProgress(100*loaded/arr.length); | |
if(loaded==arr.length)fnSucc&&fnSucc(); | |
this.onload=this.onerror=null; | |
this.src=''; | |
}; | |
oImg.onerror=function (){ | |
fnFaild&&fnFaild(this.src); | |
fnFaild=fnSucc=fnProgress=null; | |
}; | |
oImg.src=arr[i]; | |
} | |
}, | |
//用新属性获取元素页面的位置getBoundingClientRect,不用循环判断他们的父级的定位 | |
getPos: function(obj) { | |
var iScrollTop = document.documentElement.scrollTop || document.body.scrollTop, | |
iScrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft, | |
iPos = obj.getBoundingClientRect(); | |
return {top: iPos.top + iScrollTop, left: iPos.left + iScrollLeft, right: iPos.right + iScrollLeft, bottom: iPos.bottom + iScrollTop} | |
}, | |
//获取元素的style | |
getStyle:function(obj, name){ | |
if(obj.currentStyle){ | |
return obj.currentStyle[name]; | |
}else{ | |
return getComputedStyle(obj, false)[name]; | |
} | |
}, | |
//判断是否含有某个class | |
hasClass:function(obj,className){ | |
return new RegExp("(^|\\s)"+className+"(\\s|$)").test(obj.className); | |
}, | |
//删除class | |
removeClass:function(obj,className) { | |
var arr=obj.className.split(/\s+/); | |
arr.forEach(function(val,i){ | |
if(val==className){ | |
arr.splice(i,1); | |
} | |
}) | |
obj.className = arr.join(" "); | |
}, | |
//添加class | |
addClass:function(obj,className){ | |
var arr=obj.className.split(/\s+/); | |
this.hasClass(obj,className) || arr.push(className); | |
obj.className=arr.join(" ").replace(/(^\s*)|(\s*$)/, ""); | |
}, | |
//绑定事件 | |
bindEvent:function(obj, ev, fn){ | |
obj.addEventListener?obj.addEventListener(ev, fn, false):obj.attachEvent('on'+ev, fn); | |
}, | |
//解绑 | |
unbindEvent:function(obj, ev, fn){ | |
obj.removeEventListener?obj.removeEventListener(ev, fn, false):obj.detachEvent('on'+ev, fn); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment