Self-encapsulated common JavaScript functions share _javascript Tips

Source: Internet
Author: User

Are some commonly used functions, there is no more nonsense, small partners to see their own understanding

The code:

Copy Code code as follows:

Cookies
function Setcookie (name, value, Iday)
{
if (Iday!==false)
{
var odate=new Date ();
Odate.setdate (Odate.getdate () +iday);
document.cookie=name+ ' = ' +value+ '; expires= ' +odate+ ';p ath=/';
}
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 random numbers
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.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;
return false;
}
};
function Fnmove (EV)
{
var oevent=ev| | Event
var l= (document.documentelement.scrollleft| | Document.body.scrollLeft) +oevent.clientx-disx;
var t= (document.documentelement.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 motion
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;
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&LT;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 motion
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
}
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;
}
}
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.