Tab toggle component (Tab functionality) instance code

  • 2020-03-29 23:56:48
  • OfStack

Direct paste code with comments:



    function Tab(){
        this.config = {
            type            : 'mouseover',    //The default is to mouse over
            autoplay        : true,           //Auto play by default
            triggerCls      : '.list',        //A menu item
            panelCls        : '.tabContent',  //Content items
            index           : 0,              //Current index 0
            switchTo        : 0,              //Let's switch to which term
            interval        : 3000,              //The default value of the auto-play interval is 3 in s
            pauseOnHover    : true,           //The default is true if the mouse is up to pause
            current         : 'current',      //The current item is added to the class name
            hidden          : 'hidden',       //The class name defaults to hidden
            callback        : null            //The callback function
        };
        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);

                //After toggling the add callback function
                _config.callback && $.isFunction(_config.callback) && _config.callback(index);
            });
            //Switch to item number by default
            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);
            }
            //Automatically play
            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);
            }
        }
    };

The method of invocation on the page is as follows:


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


Related articles: