国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

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

2019-11-20 21:37:32
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

直接貼代碼里面有注釋?zhuān)?/P>

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

/**
 * 簡(jiǎn)單的Tab切換
 * 支持可配置項(xiàng) 如下參數(shù)
 */
    function Tab(){
        this.config = {
            type            : 'mouseover',    //類(lèi)型 默認(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)添加到類(lèi)名
            hidden          : 'hidden',       // 類(lèi)名 默認(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);

            }
        }
    };

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

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

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

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 丹东市| 崇仁县| 南召县| 马公市| 台中市| 大邑县| 大姚县| 正蓝旗| 延川县| 阳春市| 抚顺市| 襄城县| 河南省| 巩义市| 林西县| 旅游| 陆川县| 郧西县| 正蓝旗| 新龙县| 怀化市| 武功县| 枞阳县| 舞钢市| 广宁县| 富蕴县| 梁山县| 武胜县| 藁城市| 康乐县| 贵南县| 苍梧县| 获嘉县| 梨树县| 贞丰县| 凉山| 静海县| 兰坪| 南华县| 浦县| 西昌市|