Share your own encapsulated common javascript Functions and encapsulate javascript Functions
These are all common functions, so there will be no more nonsense here. Let's take a look.
Code:
Copy codeThe Code is as follows:
// Cookie
Function setCookie (name, value, iDay)
{
If (iDay! = False)
{
Var oDate = new Date ();
ODate. setDate (oDate. getDate () + iDay );
Document. cookie = name + '=' + value + '; expires =' + oDate + '; path = /';
}
Else
{
Document. cookie = name + '=' + value;
}
}
Function getCookie (name)
{
Var arr = document. cookie. split (';');
Var I = 0;
For (I = 0; I <arr. length; I ++)
{
Var arr2 = arr [I]. split ('= ');
If (arr2 [0] = name)
{
Return arr2 [1];
}
}
Return '';
}
Function removeCookie (name)
{
SetCookie (name, 'A',-1 );
}
// Event
Function myAddEvent (obj, ev, fn ){
Obj. attachEvent? Obj. attachEvent ('on' + ev, fn): obj. addEventListener (ev, fn, false );
}
Function myDelEvent (obj, ev, fn ){
Obj. detachEvent? Obj. detachEvent ('on' + ev, fn): obj. removeEventListener (ev, fn, false );
}
Function getByClass (oParent, sClass)
{
Var aEle = oParent. getElementsByTagName ('*');
Var re = new RegExp ('\ B' + sClass + '\ B', 'I ');
Var aResult = [];
For (var I = 0; I <aEle. length; I ++)
{
If (re. test (aEle [I]. className ))
{
AResult. push (aEle [I]);
}
}
Return aResult;
}
Function bindEvent (obj, ev, fn)
{
Obj. addEventListener? Obj. addEventListener (ev, fn, false): obj. attachEvent ('on' + ev, fn );
}
Function unbindEvent (obj, ev, fn)
{
Obj. removeEventListener? Obj. removeEventListener (ev, fn, false): obj. detachEvent ('on' + ev, fn );
}
// Generate a random number
Function rnd (n, m)
{
Return Math. random () * (m-n) + n;
}
Function time2date (t)
{
Function d (n) {return n <10? '0' + n: ''+ n ;}
Var oDate = new Date (t * 1000 );
Return oDate. getFullYear () + '-' + d (oDate. getMonth () + 1) + '-' + d (oDate. getDate () + ''+ d (oDate. getHours () + ':' + d (oDate. getMinutes () + ':' + d (oDate. getSeconds ());
}
Function time2day (t)
{
Function d (n) {return n <10? '0' + n: ''+ n ;}
Var oDate = new Date (t * 1000 );
Return oDate. getFullYear () + '-' + d (oDate. getMonth () + 1) + '-' + d (oDate. getDate ());
}
// Drag
Function drag (objEv, objMove, fnMoveCallBack)
{
Var disX = 0, disY = 0;
ObjEv. onmousedown = function (ev)
{
Var oEvent = ev | event;
Disx((document.doc umentElement. scrollLeft | document. body. scrollLeft) + oEvent. clientX-objMove.offsetLeft;
Disycategory (document.doc umentElement. 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;
Return false;
}
};
Function fnMove (ev)
{
Var oEvent = ev | event;
Var l=(document.doc umentElement. scrollLeft | document. body. scrollLeft) + oEvent. clientX-disX;
Var t=(document.doc umentElement. scrollTop | document. body. scrollTop) + oEvent. clientY-disY;
FnMoveCallBack (l, t );
}
Function fnUp ()
{
This. onmousemove = null;
This. onmouseup = null;
If (this. releaseCapture) this. releaseCapture ();
}
}
Function mouseScroll (obj, fnCallBack)
{
BindEvent (obj, 'mousewheel ', fnScroll );
BindEvent (obj, 'dommousescroll', fnScroll );
Function fnScroll (ev)
{
Var oEvent = ev | event;
Var bDown;
If (oEvent. wheelDelta)
{
BDown = oEvent. wheelDelta <0;
}
Else
{
BDown = oEvent. detail> 0;
}
FnCallBack (bDown );
If (oEvent. preventDefault) oEvent. preventDefault ();
Return false;
}
}
// Swing
ZnO. site. fx. swing = function (obj, cur, target, fnDo, fnEnd, acc)
{
If (ZnO. site. fx. browser_test.IE6)
{
FnDo & fnDo. call (obj, target );
FnEnd & fnEnd. call (obj, target );
Return;
}
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;
}
}
};
// Elastic movement
ZnO. site. fx. flex = function (obj, cur, target, fnDo, fnEnd, fs, MS)
{
If (ZnO. site. fx. browser_test.IE6)
{
FnDo & fnDo. call (obj, target );
FnEnd & fnEnd. call (obj, target );
Return;
}
Var MAX_SPEED = 16;
If (! Fs) fs = 6;
If (! MS) MS = 0.75;
Var now = {};
Var x = 0; // 0-100
If (! Obj. _ flex_v) obj. _ flex_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 (){
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 (var I in cur)
{
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;
}
}
};