自己封装的常用javascript函数分享
都是些常用的功能,这里就不多废话了,小伙伴们自己看下就明白了
奉上代码:
//cookie
functionsetCookie(name,value,iDay)
{
if(iDay!==false)
{
varoDate=newDate();
oDate.setDate(oDate.getDate()+iDay);
document.cookie=name+'='+value+';expires='+oDate+';path=/';
}
else
{
document.cookie=name+'='+value;
}
}
functiongetCookie(name)
{
vararr=document.cookie.split(';');
vari=0;
for(i=0;i<arr.length;i++)
{
vararr2=arr[i].split('=');
if(arr2[0]==name)
{
returnarr2[1];
}
}
return'';
}
functionremoveCookie(name)
{
setCookie(name,'a',-1);
}
//事件
functionmyAddEvent(obj,ev,fn){
obj.attachEvent?obj.attachEvent('on'+ev,fn):obj.addEventListener(ev,fn,false);
}
functionmyDelEvent(obj,ev,fn){
obj.detachEvent?obj.detachEvent('on'+ev,fn):obj.removeEventListener(ev,fn,false);
}
functiongetByClass(oParent,sClass)
{
varaEle=oParent.getElementsByTagName('*');
varre=newRegExp('\\b'+sClass+'\\b','i');
varaResult=[];
for(vari=0;i<aEle.length;i++)
{
if(re.test(aEle[i].className))
{
aResult.push(aEle[i]);
}
}
returnaResult;
}
functionbindEvent(obj,ev,fn)
{
obj.addEventListener?obj.addEventListener(ev,fn,false):obj.attachEvent('on'+ev,fn);
}
functionunbindEvent(obj,ev,fn)
{
obj.removeEventListener?obj.removeEventListener(ev,fn,false):obj.detachEvent('on'+ev,fn);
}
//生成随机数
functionrnd(n,m)
{
returnMath.random()*(m-n)+n;
}
functiontime2date(t)
{
functiond(n){returnn<10?'0'+n:''+n;}
varoDate=newDate(t*1000);
returnoDate.getFullYear()+'-'+d(oDate.getMonth()+1)+'-'+d(oDate.getDate())+''+d(oDate.getHours())+':'+d(oDate.getMinutes())+':'+d(oDate.getSeconds());
}
functiontime2day(t)
{
functiond(n){returnn<10?'0'+n:''+n;}
varoDate=newDate(t*1000);
returnoDate.getFullYear()+'-'+d(oDate.getMonth()+1)+'-'+d(oDate.getDate());
}
//拖拽
functiondrag(objEv,objMove,fnMoveCallBack)
{
vardisX=0,disY=0;
objEv.onmousedown=function(ev)
{
varoEvent=ev||event;
disX=(document.documentElement.scrollLeft||document.body.scrollLeft)+oEvent.clientX-objMove.offsetLeft;
disY=(document.documentElement.scrollTop||document.body.scrollTop)+oEvent.clientY-objMove.offsetTop;
if(objEv.setCapture)
{
objEv.onmousemove=fnMove;
objEv.onmouseup=fnUp;
objEv.setCapture();
}
else
{
document.onmousemove=fnMove;
document.onmouseup=fnUp;
returnfalse;
}
};
functionfnMove(ev)
{
varoEvent=ev||event;
varl=(document.documentElement.scrollLeft||document.body.scrollLeft)+oEvent.clientX-disX;
vart=(document.documentElement.scrollTop||document.body.scrollTop)+oEvent.clientY-disY;
fnMoveCallBack(l,t);
}
functionfnUp()
{
this.onmousemove=null;
this.onmouseup=null;
if(this.releaseCapture)this.releaseCapture();
}
}
functionmouseScroll(obj,fnCallBack)
{
bindEvent(obj,'mousewheel',fnScroll);
bindEvent(obj,'DOMMouseScroll',fnScroll);
functionfnScroll(ev)
{
varoEvent=ev||event;
varbDown;
if(oEvent.wheelDelta)
{
bDown=oEvent.wheelDelta<0;
}
else
{
bDown=oEvent.detail>0;
}
fnCallBack(bDown);
if(oEvent.preventDefault)oEvent.preventDefault();
returnfalse;
}
}
//摆动运动
zns.site.fx.swing=function(obj,cur,target,fnDo,fnEnd,acc)
{
if(zns.site.fx.browser_test.IE6)
{
fnDo&&fnDo.call(obj,target);
fnEnd&&fnEnd.call(obj,target);
return;
}
if(!acc)acc=0.1;
varnow={};
varx=0; //0-100
if(!obj.__swing_v)obj.__swing_v=0;
if(!obj.__last_timer)obj.__last_timer=0;
vart=newDate().getTime();
if(t-obj.__last_timer>20)
{
fnMove();
obj.__last_timer=t;
}
clearInterval(obj.timer);
obj.timer=setInterval(fnMove,20);
functionfnMove(){
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(variincur)
{
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;
}
}
};
//弹性运动
zns.site.fx.flex=function(obj,cur,target,fnDo,fnEnd,fs,ms)
{
if(zns.site.fx.browser_test.IE6)
{
fnDo&&fnDo.call(obj,target);
fnEnd&&fnEnd.call(obj,target);
return;
}
varMAX_SPEED=16;
if(!fs)fs=6;
if(!ms)ms=0.75;
varnow={};
varx=0; //0-100
if(!obj.__flex_v)obj.__flex_v=0;
if(!obj.__last_timer)obj.__last_timer=0;
vart=newDate().getTime();
if(t-obj.__last_timer>20)
{
fnMove();
obj.__last_timer=t;
}
clearInterval(obj.timer);
obj.timer=setInterval(fnMove,20);
functionfnMove(){
obj.__flex_v+=(100-x)/fs;
obj.__flex_v*=ms;
if(Math.abs(obj.__flex_v)>MAX_SPEED)obj.__flex_v=obj.__flex_v>0?MAX_SPEED:-MAX_SPEED;
x+=obj.__flex_v;
for(variincur)
{
now[i]=(target[i]-cur[i])*x/100+cur[i];
}
if(fnDo)fnDo.call(obj,now);
if(Math.abs(obj.__flex_v)<1&&Math.abs(100-x)<1)
{
clearInterval(obj.timer);
if(fnEnd)fnEnd.call(obj,target);
obj.__flex_v=0;
}
}
};