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

Tab切換組件(選項(xiàng)卡功能)實(shí)例代碼

 更新時(shí)間:2013年11月21日 09:54:33   作者:  
這篇文章主要介紹了一個(gè)簡單的Tab切換組件實(shí)例,大家可以參考使用

直接貼代碼里面有注釋:

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

/**
 * 簡單的Tab切換
 * 支持可配置項(xiàng) 如下參數(shù)
 */
    function Tab(){
        this.config = {
            type            : 'mouseover',    //類型 默認(rèn)為鼠標(biāo)移上去
            autoplay        : true,           // 默認(rèn)為自動(dòng)播放
            triggerCls      : '.list',        // 菜單項(xiàng)
            panelCls        : '.tabContent',  // 內(nèi)容項(xiàng)
            index           : 0,              // 當(dāng)前的索引0
            switchTo        : 0,              // 切換到哪一項(xiàng)
            interval        : 3000,              // 自動(dòng)播放間隔時(shí)間 默認(rèn)為3 以s為單位
            pauseOnHover    : true,           // 鼠標(biāo)放上去是否為暫停 默認(rèn)為true
            current         : 'current',      // 當(dāng)前項(xiàng)添加到類名
            hidden          : 'hidden',       // 類名 默認(rèn)為hidden
            callback        : null            // callback函數(shù)
        };

        this.cache = {
            timer : undefined,
            flag  : true
        };
    }

    Tab.prototype = {

        init: function(options){
            this.config = $.extend(this.config,options || {});
            var self = this,
                _config = self.config;
            self._handler();
        },
        _handler: function(){
            var self = this,
                _config = self.config,
                _cache = self.cache,
                len = $(_config.triggerCls).length;
            $(_config.triggerCls).unbind(_config.type);
            $(_config.triggerCls).bind(_config.type,function(){
                _cache.timer && clearInterval(_cache.timer);
                var index = $(_config.triggerCls).index(this);
                !$(this).hasClass(_config.current) &&
                $(this).addClass(_config.current).siblings().removeClass(_config.current);
                $(_config.panelCls).eq(index).removeClass(_config.hidden).siblings().addClass(_config.hidden);

                // 切換完 添加回調(diào)函數(shù)
                _config.callback && $.isFunction(_config.callback) && _config.callback(index);
            });

            // 默認(rèn)情況下切換到第幾項(xiàng)
            if(_config.switchTo) {
                $(_config.triggerCls).eq(_config.switchTo).addClass(_config.current).siblings().removeClass(_config.current);
                $(_config.panelCls).eq(_config.switchTo).removeClass(_config.hidden).siblings().addClass(_config.hidden);
            }

            // 自動(dòng)播放
            if(_config.autoplay) {
                start();
                $(_config.triggerCls).hover(function(){
                    if(_config.pauseOnHover) {
                        _cache.timer && clearInterval(_cache.timer);
                        _cache.timer = undefined;
                    }else {
                        return;
                    }
                },function(){
                    start();
                });
            }
            function start(){
                _cache.timer = setInterval(autoRun,_config.interval);
            }
            function autoRun() {
                if(_config.switchTo && (_config.switchTo == len-1)){
                    if(_cache.flag) {
                        _config.index = _config.switchTo;
                        _cache.flag = false;
                    }
                }
                _config.index++;
                if(_config.index == len) {
                    _config.index = 0;
                }
                $(_config.triggerCls).eq(_config.index).addClass(_config.current).siblings().removeClass(_config.current);
                $(_config.panelCls).eq(_config.index).removeClass(_config.hidden).siblings().addClass(_config.hidden);

            }
        }
    };

頁面上調(diào)用方式如下:

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

$(function(){
    new Tab().init({
        switchTo: 1,
        callback: function(index){
            console.log(index);
        }
    });
});

相關(guān)文章

最新評(píng)論