Maison  >  Article  >  interface Web  >  Partagez des fonctions javascript communes encapsulées par yourself_javascript skills

Partagez des fonctions javascript communes encapsulées par yourself_javascript skills

WBOY
WBOYoriginal
2016-05-16 16:21:53893parcourir

Ce sont quelques fonctions couramment utilisées, donc je n'entrerai pas dans trop de bêtises ici, vous comprendrez en les regardant vous-même

Fournir le code :

Copier le code Le code est le suivant :

//cookie
fonction setCookie(nom, valeur, iDay)
{
    si(iDay!==false)
    {
        var oDate=nouvelle Date();
        oDate.setDate(oDate.getDate() iDay);
        document.cookie=nom '=' valeur ';expires=' oDate ';path=/';
    >
    d'autre
    {
        document.cookie=nom '=' valeur;
    >
>
fonction getCookie(nom)
{
    var arr=document.cookie.split('; ');
    var je=0;
    pour(i=0;i     {
        var arr2=arr[i].split('=');
        if(arr2[0]==nom)
        {
            retourner arr2[1];
        >
    >
    retourner '';
>
fonction removeCookie(nom)
{
    setCookie(nom, 'a', -1);
>
//事件
fonction monAjouteÉvénement(obj, ev, fn){
    obj.attachEvent?obj.attachEvent('on' ev, fn):obj.addEventListener(ev, fn, false);
>
fonction monDelEvent(obj, ev, fn){
    obj.detachEvent?obj.detachEvent('on' ev, fn):obj.removeEventListener(ev, fn, false);
>
fonction getByClass(oParent, sClass)
{
    var aEle=oParent.getElementsByTagName('*');
    var re=new RegExp('\b' sClass '\b', 'i');
    var aResult=[];
    pour(var i=0;i     {
        if(re.test(aEle[i].className))
        {
            aResult.push(aEle[i]);
        >
    >
    renvoyer un résultat ;
>
fonction bindEvent(obj, ev, fn)
{
    obj.addEventListener?obj.addEventListener(ev, fn, false):obj.attachEvent('on' ev, fn);
>
fonction unbindEvent(obj, ev, fn)
{
    obj.removeEventListener?obj.removeEventListener(ev, fn, false):obj.detachEvent('on' ev, fn);
>
//生成随机数
fonction rnd(n, m)
{
    return Math.random()*(m-n) n;
>
fonction time2date(t)
{
    fonction d(n){return n<10?'0' n:'' n;}
    var oDate=nouvelle Date(t*1000);
    return oDate.getFullYear() '-' d(oDate.getMonth() 1) '-' d(oDate.getDate()) ' ' d(oDate.getHours()) ':' d(oDate.getMinutes()) ':' d(oDate.getSeconds());
>
fonction time2day(t)
{
    fonction d(n){return n<10?'0' n:'' n;}
    var oDate=nouvelle Date(t*1000);
    return oDate.getFullYear() '-' d(oDate.getMonth() 1) '-' d(oDate.getDate());
>
//拖拽
fonction glisser(objEv, objMove, fnMoveCallBack)
{
    var disX=0,disY=0;
    objEv.onmousedown=fonction (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;
        si(objEv.setCapture)
        {
            objEv.onmousemove=fnMove;
            objEv.onmouseup=fnUp;
            objEv.setCapture();
        >
        d'autre
        {
            document.onmousemove=fnMove;
            document.onmouseup=fnUp;
            retourner faux ;
        >
    };
    fonction 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);
    >
    fonction fnUp()
    {
        this.onmousemove=null;
        this.onmouseup=null;
        if(this.releaseCapture)this.releaseCapture();
    >
>
fonction mouseScroll(obj, fnCallBack)
{
    bindEvent(obj, 'mousewheel', fnScroll);
    bindEvent(obj, 'DOMMouseScroll', fnScroll);
    fonction fnScroll(ev)
    {
        var oEvent=ev||event;
        var bDown;
        si(oEvent.wheelDelta)
        {
            bDown=oEvent.wheelDelta<0;
        >
        d'autre
        {
            bDown=oEvent.detail>0;
        >
        fnCallBack(bDown);
        if(oEvent.preventDefault)oEvent.preventDefault();
        retourner faux ;
    >
>
//摆动运动
zns.site.fx.swing=fonction (obj, cur, target, fnDo, fnEnd, acc)
{
    si(zns.site.fx.browser_test.IE6)
    {
        fnDo&&fnDo.call(obj, cible);
        fnEnd&&fnEnd.call(obj, cible);
        revenir ;
    >
    si(!acc)acc=0.1;
    var maintenant={};
    variable x=0 ;    //0-100
    si(!obj.__swing_v)obj.__swing_v=0;
    si(!obj.__last_timer)obj.__last_timer=0;
    var t=new Date().getTime();
    si(t-obj.__last_timer>20)
    {
        fnMove();
        obj.__last_timer=t;
    >
    clearInterval(obj.timer);
    obj.timer=setInterval(fnMove, 20);
    fonction fnMove(){
        si(x<50)
        {
            obj.__swing_v =acc;
        >
        d'autre
        {
            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;
        //alerte(x ',' obj.__swing_v);
        pour(var i in cur)
        {
            maintenant[i]=(target[i]-cur[i])*x/100 cur[i];
        >
        if(fnDo)fnDo.call(obj, maintenant);
        si(/*Math.abs(obj.__swing_v)<1 || */Math.abs(100-x)<1)
        {
            clearInterval(obj.timer);
            if(fnEnd)fnEnd.call(obj, cible);
            obj.__swing_v=0;
        >
    >
};
//弹性运动
zns.site.fx.flex=函數 (obj, cur, target, fnDo, fnEnd, fs, ms)
{
    if(zns.site.fx.browser_test.IE6)
    {
        fnDo&&fnDo.call(obj, target);
        fnEnd&&fnEnd.call(obj, target);
        返回;
    }
    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);
    函數 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)         {
            clearInterval(obj.timer);
            if(fnEnd)fnEnd.call(obj, target);
            obj.__flex_v=0;
        }
    }
};
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn