当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Tab切换组件(选项卡功能)实例代码

Tab切换组件(选项卡功能)实例代码

2019年03月19日  | 移动技术网IT编程  | 我要评论

直接贴代码里面有注释:

 

. 代码如下:


/**
 * 简单的tab切换
 * 支持可配置项 如下参数
 */
    function tab(){
        this.config = {
            type            : 'mouver',    //类型 默认为鼠标移上去
            autoplay        : true,           // 默认为自动播放
            triggercls      : '.list',        // 菜单项
            panelcls        : '.tabcontent',  // 内容项
            index           : 0,              // 当前的索引0
            switchto        : 0,              // 切换到哪一项
            interval        : 3000,              // 自动播放间隔时间 默认为3 以s为单位
            pauseonhover    : true,           // 鼠标放上去是否为暂停 默认为true
            current         : 'current',      // 当前项添加到类名
            hidden          : 'hidden',       // 类名 默认为hidden
            callback        : null            // callback函数
        };

 

        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);

                // 切换完 添加回调函数
                _config.callback && $.isfunction(_config.callback) && _config.callback(index);
            });

            // 默认情况下切换到第几项
            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);
            }

            // 自动播放
            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);

            }
        }
    };

 

页面上调用方式如下:

 

. 代码如下:


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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网