国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 舞阳县| 雷山县| 鄄城县| 满洲里市| 宝清县| 松潘县| 张家港市| 古蔺县| 连城县| 博客| 成安县| 将乐县| 延津县| 竹溪县| 登封市| 凤阳县| 黎城县| 基隆市| 托里县| 青神县| 清水河县| 南漳县| 九龙县| 灵丘县| 淮滨县| 抚远县| 黄浦区| 政和县| 遂宁市| 长汀县| 诸城市| 石河子市| 木兰县| 栾川县| 海城市| 禄丰县| 巍山| 东城区| 恭城| 鄂尔多斯市| 肇源县|