欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js動(dòng)畫(huà)(animate)簡(jiǎn)單引擎代碼示例

 更新時(shí)間:2012年12月04日 15:47:59   作者:  
仿照f(shuō)lash的動(dòng)畫(huà)原理,自己寫(xiě)了一個(gè)非常簡(jiǎn)單的js動(dòng)畫(huà)引擎。

用慣了jquery的同學(xué),相信都很欣賞其動(dòng)畫(huà)引擎。確實(shí)相對(duì)比較完善!如果,如果想像力足夠豐富的話,相信可以做出超出想像的效果。當(dāng)然,跟2d庫(kù)比起來(lái),還是相差相當(dāng)一段距離。jquery壓根也不是專門(mén)為動(dòng)畫(huà)而設(shè)計(jì)的。模擬真實(shí)世界方面,還是不足的。但在web世界里還是游刃有余的。動(dòng)畫(huà)其實(shí)一直是flash的專屬領(lǐng)地(web區(qū)哉)。只是它常常淪為黑客攻擊的漏洞所在,而且要裝插件,有時(shí)候文件實(shí)在太大,而且性耗實(shí)在是高啊。html5出現(xiàn)后,其實(shí)adobe自己都轉(zhuǎn)移陣地到html5了。當(dāng)然,我覺(jué)得很長(zhǎng)一段時(shí)間內(nèi),flash是不會(huì)被放棄的。

長(zhǎng)話短說(shuō),步入正題。仿照f(shuō)lash的動(dòng)畫(huà)原理,自己寫(xiě)了一個(gè)非常簡(jiǎn)單的js動(dòng)畫(huà)引擎。

首先,用過(guò)flash的同學(xué)都知道。flash有個(gè)時(shí)間線,上面布滿了“幀”。其實(shí)每個(gè)幀都是一個(gè)鏡頭,鏡頭連貫起來(lái)就是一副動(dòng)畫(huà)效果。其實(shí),這跟電影的原理也一樣的,小時(shí)候玩過(guò)膠片的都知道,對(duì)著光可以看到一副副的鏡頭。人眼分辨兩個(gè)畫(huà)面的連貫是有時(shí)間限度的。如果想看不到兩個(gè)畫(huà)面變換時(shí)的閃爍大概30幀/秒左右,電影是24幀的。所以,如果能保證,動(dòng)畫(huà)切換能保證每秒30次,基本上,就做到了動(dòng)畫(huà)的流暢效果,但是這取決環(huán)境。所以具體情況,具體而定,其實(shí)關(guān)于這方面的知識(shí)我也是一知半解。就這個(gè)動(dòng)畫(huà)引擎而言,了解這么多也差不多足夠了,有興趣可以查找相關(guān)知識(shí)!

下面開(kāi)始說(shuō)說(shuō)設(shè)計(jì)原理。

首先需要一個(gè)幀頻,也就是多少幀每秒。如果有了總用時(shí),就可以計(jì)算出整個(gè)動(dòng)畫(huà)下來(lái)有多少個(gè)“畫(huà)面”(總幀數(shù))。這種設(shè)計(jì),顯然有個(gè)不足的地方,不能保證時(shí)間正好是個(gè)整數(shù)幀。除非1ms一幀。這是一個(gè)網(wǎng)友提出來(lái)的,我感覺(jué)不好就沒(méi)有采用。所以這個(gè)引擎是有時(shí)間誤差的。有了總幀數(shù),當(dāng)動(dòng)畫(huà)運(yùn)行到最后一幀的時(shí)候,整個(gè)動(dòng)畫(huà)也就播放完。如果需要重復(fù)播放,重新把當(dāng)前幀歸0。這種動(dòng)畫(huà)有一個(gè)好處,就可以直接運(yùn)行到指定幀。也就是flash里的gotoAndStop和gotoAndPlay。其實(shí)整個(gè)動(dòng)畫(huà)設(shè)計(jì)原理都是照著flash實(shí)現(xiàn)的。包括一個(gè)很重要的方法:enterFrame。位置就是根據(jù)進(jìn)入當(dāng)前幀來(lái)計(jì)算的。還有其它一些方法:stop、play、next......等等因?yàn)槟壳皝?lái)說(shuō),這個(gè)引擎是寫(xiě)非常簡(jiǎn)單和粗糙的,先不說(shuō)這么詳細(xì)了。下面先上代碼和示例吧!

animate.js 動(dòng)畫(huà)核心

復(fù)制代碼 代碼如下:

var animation = function(obj) {
    this.obj = obj;
    this.frames = 0;
    this.timmer = undefined;
    this.running = false;
    this.ms = [];
}

animation.prototype = {
    fps: 36,
    init: function(props, duration, tween) {
        //console.log('初始化');
        this.curframe = 0;
        this.initstate = {};
        this.props = props;
        this.duration = duration || 1000;
        this.tween = tween || function(t, b, c, d) {
            return t * c / d + b;
        };
        this.frames = Math.ceil(this.duration * this.fps/1000);
        for (var prop in this.props) {
            this.initstate[prop] = {
                from: parseFloat($util.dom.getStyle(this.obj, prop)),
                to: parseFloat(this.props[prop])
            };
        }
    },
    start: function() {
        if (!this.running && this.hasNext()) {
            //console.log('可以執(zhí)行...');
            this.ms.shift().call(this)
        }
        return this;
    },
    //開(kāi)始播放
    play: function(callback) {
        //console.log('開(kāi)始動(dòng)畫(huà)!');
        var that = this;

        this.running = true;

        if (this.timmer) {
            this.stop();
        }

        this.timmer = setInterval(function() {
            if (that.complete()) {
                that.stop();
                that.running = false;
                if (callback) {
                    callback.call(that);
                }
                return;
            }
            that.curframe++;
            that.enterFrame.call(that);
        },
 / this.fps);

        return this;
    },
    // 停止動(dòng)畫(huà)
    stop: function() {
        //console.log('結(jié)束動(dòng)畫(huà)!');
        if (this.timmer) {
            clearInterval(this.timmer);
            // 清除掉timmer id
            this.timmer = undefined;
        }

    },
    go: function(props, duration, tween) {
        var that = this;
        //console.log(tween)
        this.ms.push(function() {
            that.init.call(that, props, duration, tween);
            that.play.call(that, that.start);
        });
        return this;
    },
    //向后一幀
    next: function() {
        this.stop();
        this.curframe++;
        this.curframe = this.curframe > this.frames ? this.frames: this.curframe;
        this.enterFrame.call(this);
    },
    //向前一幀
    prev: function() {
        this.stop();
        this.curframe--;
        this.curframe = this.curframe < 0 ? 0 : this.curframe;
        this.enterFrame.call(this);
    },
    //跳躍到指定幀并播放
    gotoAndPlay: function(frame) {
        this.stop();
        this.curframe = frame;
        this.play.call(this);
    },
    //跳到指定幀停止播放
    gotoAndStop: function(frame) {
        this.stop();
        this.curframe = frame;
        this.enterFrame.call(this);
    },
    //進(jìn)入幀動(dòng)作
    enterFrame: function() {
        //console.log('進(jìn)入幀:' + this.curframe)
        var ds;
        for (var prop in this.initstate) {
            //console.log('from: ' + this.initstate[prop]['from'])
            ds = this.tween(this.curframe, this.initstate[prop]['from'], this.initstate[prop]['to'] - this.initstate[prop]['from'], this.frames).toFixed(2);
            //console.log(prop + ':' + ds)
            $util.dom.setStyle(this.obj, prop, ds)
        }
    },
    //動(dòng)畫(huà)結(jié)束
    complete: function() {
        return this.curframe >= this.frames;
    },
    hasNext: function() {
        return this.ms.length > 0;
    }
}


下面是一個(gè)簡(jiǎn)單的工具,其中有所用到的緩動(dòng)公式:

util.js

復(fù)制代碼 代碼如下:

$util = {
    /**
    * 類(lèi)型檢測(cè)
    */
    type : function(obj){
        var rep = /\[object\s+(\w+)\]/i;
        var str = Object.prototype.toString.call(obj).toLowerCase();
        str.match(rep);
        return RegExp.$1;
    },
    /**
    * 深拷貝
    */
    $unlink :function (object){
        var unlinked;
        switch ($type(object)){
            case 'object':
                unlinked = {};
                for (var p in object) {
                    unlinked[p] = $unlink(object[p]);
                }
            break;
            case 'array':
                unlinked = [];
                for (var i = 0, l = object.length; i < l; i++) {
                    unlinked[i] = $unlink(object[i]);
                }
            break;
            default: return object;
        }
        return unlinked;
    },
    /**
    *Dom 相關(guān)操作
    */
    dom:{
        $: function(id) {
            return document.getElementById(id);
        },
        getStyle: function(obj, prop) {
            var style = obj.currentStyle || window.getComputedStyle(obj, '');
            if (obj.style.filter) {
                return obj.style.filter.match(/\d+/g)[0];
            }
            return style[prop];
        },
        setStyle: function(obj, prop, val) {
            switch (prop) {
            case 'opacity':
                if($util.client.browser.ie){
                    obj.style.filter = 'alpha(' + prop + '=' + val*100 + ')'   
                }else{
                    obj.style[prop] = val;
                }
                break;
            default:
                obj.style[prop] = val + 'px';
                break;
            }
        },
        setStyles: function(obj, props) {
            for (var prop in props) {
                switch (prop) {
                case 'opacity':
                    if($util.client.browser.ie){
                        obj.style.filter = 'alpha(' + prop + '=' + props[prop] + ')'       
                    }else{
                        obj.style[prop] = props[prop];
                    }
                    break;
                default:
                    obj.style[prop] = props[prop] + 'px';
                    break;
                }
            }
        }
    },
    /**
    *Event 事件相關(guān)
    */
    evt : {
        addEvent : function(oTarget, sEventType, fnHandler) {
            if (oTarget.addEventListener) {
                oTarget.addEventListener(sEventType, fnHandler, false);
            } else if (oTarget.attachEvent) {
                oTarget.attachEvent("on" + sEventType, fnHandler);
            } else {
                oTarget["on" + sEventType] = fnHandler;
            }
        },
        rmEvent : function removeEventHandler (oTarget, sEventType, fnHandler) {
            if (oTarget.removeEventListener) {
                oTarget.removeEventListener(sEventType, fnHandler, false);
            } else if (oTarget.detachEvent) {
                oTarget.detachEvent("on" + sEventType, fnHandler);
            } else {
                oTarget["on" + sEventType] = null;
            }
        }
    },
    /**
    *Ajax 異步加載
    */
    ajax : {
        request:function (options) {
                var xhr, res;
                var url = options.url,
                    context = options.context,
                    success = options.success,
                    type = options.type,
                    datatype = options.datatype,
                    async = options.async,
                    send = options.send,
                    headers = options.headers;

                try {
                    xhr = new XMLHttpRequest();
                } catch(e) {
                    try {
                        xhr = new ActiveXObject('MSXML2.XMLHTTP');
                    } catch(e) {
                        xhr = new ActiveXObject('Microsoft.XMLHTTP');
                    }
                }

                xhr.onreadystatechange = function() {
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        res = xhr.responseText;
                        success(res);
                    }
                }
                xhr.open(type, url, async);
                xhr.send(send);
        }
    },
    /**
    *Array 數(shù)組相關(guān)
    */
    array : {
        minIndex : function(ary){
            return Math.min.apply(null,ary);   
        },
        maxitem : function(ary){
            return Math.max.apply(null,ary);
        }
    },
    /**
    *Client 客戶端檢測(cè)
    */
    client : function(){
        // 瀏覽器渲染引擎 engines
        var engine = {           
            ie: 0,
            gecko: 0,
            webkit: 0,
            khtml: 0,
            opera: 0,

            //complete version
            ver: null 
        };

        // 瀏覽器
        var browser = {
            //browsers
            ie: 0,
            firefox: 0,
            safari: 0,
            konq: 0,
            opera: 0,
            chrome: 0,
            //specific version
            ver: null
        };

        // 客戶端平臺(tái)platform/device/OS
        var system = {
            win: false,
            mac: false,
            x11: false,

            //移動(dòng)設(shè)備
            iphone: false,
            ipod: false,
            ipad: false,
            ios: false,
            android: false,
            nokiaN: false,
            winMobile: false,

            //game systems
            wii: false,
            ps: false
        };   

        // 檢測(cè)瀏覽器引擎
        var ua = navigator.userAgent;   
        if (window.opera){
            engine.ver = browser.ver = window.opera.version();
            engine.opera = browser.opera = parseFloat(engine.ver);
        } else if (/AppleWebKit\/(\S+)/.test(ua)){
            engine.ver = RegExp["$1"];
            engine.webkit = parseFloat(engine.ver);

            //figure out if it's Chrome or Safari
            if (/Chrome\/(\S+)/.test(ua)){
                browser.ver = RegExp["$1"];
                browser.chrome = parseFloat(browser.ver);
            } else if (/Version\/(\S+)/.test(ua)){
                browser.ver = RegExp["$1"];
                browser.safari = parseFloat(browser.ver);
            } else {
                //approximate version
                var safariVersion = 1;
                if (engine.webkit < 100){
                    safariVersion = 1;
                } else if (engine.webkit < 312){
                    safariVersion = 1.2;
                } else if (engine.webkit < 412){
                    safariVersion = 1.3;
                } else {
                    safariVersion = 2;
                }  

                browser.safari = browser.ver = safariVersion;       
            }
        } else if (/KHTML\/(\S+)/.test(ua) || /Konqueror\/([^;]+)/.test(ua)){
            engine.ver = browser.ver = RegExp["$1"];
            engine.khtml = browser.konq = parseFloat(engine.ver);
        } else if (/rv:([^\)]+)\) Gecko\/\d{8}/.test(ua)){   
            engine.ver = RegExp["$1"];
            engine.gecko = parseFloat(engine.ver);

            //determine if it's Firefox
            if (/Firefox\/(\S+)/.test(ua)){
                browser.ver = RegExp["$1"];
                browser.firefox = parseFloat(browser.ver);
            }
        } else if (/MSIE ([^;]+)/.test(ua)){   
            engine.ver = browser.ver = RegExp["$1"];
            engine.ie = browser.ie = parseFloat(engine.ver);
        }

        //detect browsers
        browser.ie = engine.ie;
        browser.opera = engine.opera;
       

        //detect platform
        var p = navigator.platform;
        system.win = p.indexOf("Win") == 0;
        system.mac = p.indexOf("Mac") == 0;
        system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);

        //detect windows operating systems
        if (system.win){
            if (/Win(?:dows )?([^do]{2})\s?(\d+\.\d+)?/.test(ua)){
                if (RegExp["$1"] == "NT"){
                    switch(RegExp["$2"]){
                        case "5.0":
                            system.win = "2000";
                            break;
                        case "5.1":
                            system.win = "XP";
                            break;
                        case "6.0":
                            system.win = "Vista";
                            break;
                        case "6.1":
                            system.win = "7";
                            break;
                        default:
                            system.win = "NT";
                            break;               
                    }                           
                } else if (RegExp["$1"] == "9x"){
                    system.win = "ME";
                } else {
                    system.win = RegExp["$1"];
                }
            }
        }

        //mobile devices
        system.iphone = ua.indexOf("iPhone") > -1;
        system.ipod = ua.indexOf("iPod") > -1;
        system.ipad = ua.indexOf("iPad") > -1;
        system.nokiaN = ua.indexOf("NokiaN") > -1;

        //windows mobile
        if (system.win == "CE"){
            system.winMobile = system.win;
        } else if (system.win == "Ph"){
            if(/Windows Phone OS (\d+.\d+)/.test(ua)){;
                system.win = "Phone";
                system.winMobile = parseFloat(RegExp["$1"]);
            }
        }

        //determine iOS version
        if (system.mac && ua.indexOf("Mobile") > -1){
            if (/CPU (?:iPhone )?OS (\d+_\d+)/.test(ua)){
                system.ios = parseFloat(RegExp.$1.replace("_", "."));
            } else {
                system.ios = 2;  //can't really detect - so guess
            }
        }

        //determine Android version
        if (/Android (\d+\.\d+)/.test(ua)){
            system.android = parseFloat(RegExp.$1);
        }

        //gaming systems
        system.wii = ua.indexOf("Wii") > -1;
        system.ps = /playstation/i.test(ua);

        //return it
        return {
            engine:     engine,
            browser:    browser,
            system:     system       
        };

    }(),
    /**
    *Tween 緩動(dòng)相關(guān)
    */
    tween: {
        Linear: function(t, b, c, d) {
            return c * t / d + b;
        },
        Quad: {
            easeIn: function(t, b, c, d) {
                return c * (t /= d) * t + b;
            },
            easeOut: function(t, b, c, d) {
                return - c * (t /= d) * (t - 2) + b;
            },
            easeInOut: function(t, b, c, d) {
                if ((t /= d / 2) < 1) return c / 2 * t * t + b;
                return - c / 2 * ((--t) * (t - 2) - 1) + b;
            }
        },
        Cubic: {
            easeIn: function(t, b, c, d) {
                return c * (t /= d) * t * t + b;
            },
            easeOut: function(t, b, c, d) {
                return c * ((t = t / d - 1) * t * t + 1) + b;
            },
            easeInOut: function(t, b, c, d) {
                if ((t /= d / 2) < 1) return c / 2 * t * t * t + b;
                return c / 2 * ((t -= 2) * t * t + 2) + b;
            }
        },
        Quart: {
            easeIn: function(t, b, c, d) {
                return c * (t /= d) * t * t * t + b;
            },
            easeOut: function(t, b, c, d) {
                return - c * ((t = t / d - 1) * t * t * t - 1) + b;
            },
            easeInOut: function(t, b, c, d) {
                if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b;
                return - c / 2 * ((t -= 2) * t * t * t - 2) + b;
            }
        },
        Quint: {
            easeIn: function(t, b, c, d) {
                return c * (t /= d) * t * t * t * t + b;
            },
            easeOut: function(t, b, c, d) {
                return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
            },
            easeInOut: function(t, b, c, d) {
                if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
                return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
            }
        },
        Sine: {
            easeIn: function(t, b, c, d) {
                return - c * Math.cos(t / d * (Math.PI / 2)) + c + b;
            },
            easeOut: function(t, b, c, d) {
                return c * Math.sin(t / d * (Math.PI / 2)) + b;
            },
            easeInOut: function(t, b, c, d) {
                return - c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
            }
        },
        Expo: {
            easeIn: function(t, b, c, d) {
                return (t == 0) ? b: c * Math.pow(2, 10 * (t / d - 1)) + b;
            },
            easeOut: function(t, b, c, d) {
                return (t == d) ? b + c: c * ( - Math.pow(2, -10 * t / d) + 1) + b;
            },
            easeInOut: function(t, b, c, d) {
                if (t == 0) return b;
                if (t == d) return b + c;
                if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
                return c / 2 * ( - Math.pow(2, -10 * --t) + 2) + b;
            }
        },
        Circ: {
            easeIn: function(t, b, c, d) {
                return - c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
            },
            easeOut: function(t, b, c, d) {
                return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
            },
            easeInOut: function(t, b, c, d) {
                if ((t /= d / 2) < 1) return - c / 2 * (Math.sqrt(1 - t * t) - 1) + b;
                return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
            }
        },
        Elastic: {
            easeIn: function(t, b, c, d, a, p) {
                if (t == 0) return b;
                if ((t /= d) == 1) return b + c;
                if (!p) p = d * .3;
                if (!a || a < Math.abs(c)) {
                    a = c;
                    var s = p / 4;
                } else var s = p / (2 * Math.PI) * Math.asin(c / a);
                return - (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
            },
            easeOut: function(t, b, c, d, a, p) {
                if (t == 0) return b;
                if ((t /= d) == 1) return b + c;
                if (!p) p = d * .3;
                if (!a || a < Math.abs(c)) {
                    a = c;
                    var s = p / 4;
                } else var s = p / (2 * Math.PI) * Math.asin(c / a);
                return (a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b);
            },
            easeInOut: function(t, b, c, d, a, p) {
                if (t == 0) return b;
                if ((t /= d / 2) == 2) return b + c;
                if (!p) p = d * (.3 * 1.5);
                if (!a || a < Math.abs(c)) {
                    a = c;
                    var s = p / 4;
                } else var s = p / (2 * Math.PI) * Math.asin(c / a);
                if (t < 1) return - .5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
                return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b;
            }
        },
        Back: {
            easeIn: function(t, b, c, d, s) {
                if (s == undefined) s = 1.70158;
                return c * (t /= d) * t * ((s + 1) * t - s) + b;
            },
            easeOut: function(t, b, c, d, s) {
                if (s == undefined) s = 1.70158;
                return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
            },
            easeInOut: function(t, b, c, d, s) {
                if (s == undefined) s = 1.70158;
                if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
                return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
            }
        },
        Bounce: {
            easeIn: function(t, b, c, d) {
                return c - Tween.Bounce.easeOut(d - t, 0, c, d) + b;
            },
            easeOut: function(t, b, c, d) {
                if ((t /= d) < (1 / 2.75)) {
                    return c * (7.5625 * t * t) + b;
                } else if (t < (2 / 2.75)) {
                    return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
                } else if (t < (2.5 / 2.75)) {
                    return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
                } else {
                    return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
                }
            },
            easeInOut: function(t, b, c, d) {
                if (t < d / 2) return Tween.Bounce.easeIn(t * 2, 0, c, d) * .5 + b;
                else return Tween.Bounce.easeOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b;
            }
        }
    }

}

下面是個(gè)應(yīng)用:

復(fù)制代碼 代碼如下:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>無(wú)標(biāo)題文檔</title>
<script src="util.js" type="text/javascript"></script>
<script src="animate.js" type="text/javascript"></script>
<style type="text/css">
    body{behavior:url("csshover.htc")}
    *{padding:0;margin:0}
    .mianbox{width:480px;margin:50px auto;height:240px;}
    .leftbtn{width:20px;background:#eee;border:1px solid #ddd;float:left;margin-right:8px;height:240px;}
    .leftbtn:hover{background:#fff}
    .rightbtn{width:20px;background:#eee;border:1px solid #ddd;float:right;height:240px;}
    .rightbtn:hover{background:#fff}
    .con{width:420px;float:left;height:240px;overflow:hidden;position:relative}
    .con ul{ white-space:nowrap;font-size:0;position:absolute;left:0;top:0;}
    .con li{display:inline; vertical-align:bottom;margin-right:10px}
</style>
</head>

<body>
    <div class="mianbox">
        <div class="leftbtn" id="leftbtn"></div>
        <div class="con">
            <ul>
                <li><img src="http://pic25.nipic.com/20121121/2969675_091845785000_2.jpg" width="205" height="240" /></li>
                <li><img src="http://pic23.nipic.com/20120818/9715349_114354586392_2.jpg" width="205" height="240" /></li>
                <li><img src="http://pic20.nipic.com/20120329/9715349_154806479182_2.jpg" width="205" height="240" /></li>
                <li><img src="http://pic20.nipic.com/20120329/9715349_162301568121_2.jpg" width="205" height="240" /></li>
                <li><img src="http://pic23.nipic.com/20120824/9715349_160522342186_2.jpg" width="205" height="240" /></li>
                <li><img src="http://pic23.nipic.com/20120824/9715349_160651385195_2.jpg" width="205" height="240" /></li>
                <li><img src="http://pic20.nipic.com/20120329/9715349_163520278182_2.jpg" width="205" height="240" /></li>
                <li><img src="http://pic23.nipic.com/20120827/9715349_110350505124_2.jpg" width="205" height="240" /></li>
            </ul>
        </div>
        <div class="rightbtn" id="rightbtn"></div>
    </div>
    <script>
        /**
        * @para obj  內(nèi)容對(duì)象
        * @para lbtn 左按鈕
        * @para rbtn 右按鈕
        * @para w      每次滾動(dòng)的寬
        * @para duration 每次運(yùn)行時(shí)間
        * @para tween 緩動(dòng)類(lèi)型
        */
        function scOnce(obj, lbtn, rbtn, w, autotime, duration,tween){
            var am = new animation(_ul),
                step=0,
                ulen = obj.scrollWidth,
                timmerm
                dr = 'left',
                that = this;
            // 最右端?
            function isr(){
                return parseInt($util.dom.getStyle(obj, 'left')) >=0;   
            }
            // 最左端?
            function isl(){
                return ulen - Math.abs(parseInt($util.dom.getStyle(obj, 'left')))-10 <= w;   
            }

            if(isr()&&isl())
            return;
            // 左移
            this.goleft = function (){
                step += -1*w;
                am.go({left:step+"px"},duration,tween).start();   
            }
            // 右移
            this.goright = function (){
                step += w;
                am.go({left:step+"px"},duration,tween).start();   
            }

            // 注冊(cè)左按鈕事件
            $util.evt.addEvent(lbtn,'click',function(){
                isl()? that.goright():that.goleft();
            })
            // 注冊(cè)右按鈕事件
            $util.evt.addEvent(rbtn,'click',function(){
                isr()? that.goleft():that.goright();
            })

            $util.evt.addEvent(rbtn,'mouseover',function(){
                clearTimeout(timmer);
            })
            $util.evt.addEvent(lbtn,'mouseover',function(){
                clearTimeout(timmer);
            })
            $util.evt.addEvent(rbtn,'mouseout',function(){
                timmer = setInterval(auto,autotime);
            })
            $util.evt.addEvent(lbtn,'mouseout',function(){
                timmer = setInterval(auto,autotime);
            })

            function auto(){
                if(isl()){
                    dr = "right";
                }

                if(isr()){
                    dr = "left";
                }
                that['go'+dr]();
            }
            timmer = setInterval(auto,autotime);
        }

        var _ul = document.getElementsByTagName('ul')[0],
            lbtn = document.getElementById('leftbtn'),
            rbtn = document.getElementById('rightbtn');
        var startgo = new scOnce(_ul,lbtn,rbtn,430,3000,500,$util.tween.Quint.easeInOut);
    </script>
</body>
</html>

 

相關(guān)文章

  • Javascript添加監(jiān)聽(tīng)與刪除監(jiān)聽(tīng)用法詳解

    Javascript添加監(jiān)聽(tīng)與刪除監(jiān)聽(tīng)用法詳解

    這篇文章主要介紹了Javascript添加監(jiān)聽(tīng)與刪除監(jiān)聽(tīng)用法,較為詳細(xì)的分析了javascript原理與用法,并補(bǔ)充說(shuō)明了事件監(jiān)聽(tīng)的兼容性問(wèn)題,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-12-12
  • JS常用的4種截取字符串方法

    JS常用的4種截取字符串方法

    在做項(xiàng)目的時(shí)候,經(jīng)常會(huì)需要截取字符串,所以常用的方法有slice()、substr()、substring()、match()方法等,下面通過(guò)示例代碼介紹四個(gè)方法的使用,感興趣的朋友跟隨小編一起看看吧
    2023-02-02
  • JavaScript降低代碼圈復(fù)雜度優(yōu)化技巧

    JavaScript降低代碼圈復(fù)雜度優(yōu)化技巧

    當(dāng)一個(gè)項(xiàng)目經(jīng)過(guò)持續(xù)迭代,不斷增加功能,逐漸變成一個(gè)復(fù)雜的產(chǎn)品時(shí),新功能的開(kāi)發(fā)變得相對(duì)困難,其中一個(gè)很大的原因是代碼復(fù)雜度高,導(dǎo)致可維護(hù)性和可讀性都很差,本文將從前端JavaScript的角度出發(fā),介紹一些有效的方法和技巧來(lái)優(yōu)化前端代碼的圈復(fù)雜度
    2023-10-10
  • js核心基礎(chǔ)之閉包的應(yīng)用實(shí)例分析

    js核心基礎(chǔ)之閉包的應(yīng)用實(shí)例分析

    這篇文章主要介紹了js核心基礎(chǔ)之閉包的應(yīng)用,結(jié)合具體實(shí)例形式分析了javascript閉包使用過(guò)程中常見(jiàn)問(wèn)題及相應(yīng)的解決方法,需要的朋友可以參考下
    2019-05-05
  • 微信小程序組件生命周期的踩坑記錄

    微信小程序組件生命周期的踩坑記錄

    這篇文章主要給大家介紹了關(guān)于微信小程序組件生命周期的踩坑記錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • javaScript實(shí)現(xiàn)放大鏡特效

    javaScript實(shí)現(xiàn)放大鏡特效

    這篇文章主要為大家詳細(xì)介紹了javaScript實(shí)現(xiàn)放大鏡特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • canvas操作插件fabric.js使用方法詳解

    canvas操作插件fabric.js使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了canvas操作插件fabric.js的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • es6數(shù)值的擴(kuò)展方法

    es6數(shù)值的擴(kuò)展方法

    這篇文章主要介紹了es6數(shù)值的擴(kuò)展方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 前端項(xiàng)目中報(bào)錯(cuò)Uncaught?(in?promise)的解決方法

    前端項(xiàng)目中報(bào)錯(cuò)Uncaught?(in?promise)的解決方法

    最近在做項(xiàng)目的時(shí)候控制臺(tái)報(bào)了一個(gè)錯(cuò)Uncaught(in promise) false,這篇文章主要給大家介紹了關(guān)于前端項(xiàng)目中報(bào)錯(cuò)Uncaught?(in?promise)的解決方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-04-04
  • uni-app微信小程序下拉多選框?qū)嵗a

    uni-app微信小程序下拉多選框?qū)嵗a

    這篇文章主要給大家介紹了關(guān)于uni-app微信小程序下拉多選框的相關(guān)資料,在通過(guò)uniapp做app開(kāi)發(fā)的時(shí)候,有場(chǎng)景需要用到下拉選擇框,需要的朋友可以參考下
    2023-08-08

最新評(píng)論