代码改变世界

简单的Tab切换组件

  龙恩0707  阅读(1349)  评论(0编辑  收藏  举报

由于代码都有注释,所以不多加解释,大家都知道的。直接贴代码:

 代码如下:

复制代码
/**
 * 简单的Tab切换
 * 支持可配置项 如下参数 
 */
    function Tab(){
        this.config = {
            type            : 'mouseover',    //类型 默认为鼠标移上去
            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);
        }
    });
});

 

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示