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

代码如下:

/**
* 简单的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);
}
});
});

简单的Tab切换组件的更多相关文章

  1. react实现的tab切换组件

    我有点想要吐槽,因为用原生的js实现起来挺简单的一个小东西,改用react来写却花了我不少时间,也许react的写法只有在复杂的web应用中才能体现出它的优势吧!不过吐槽归吐槽,对react这种优雅的 ...

  2. react简单的tab切换 (styled-components)

    其实,在我们日常的编程当中,经常会碰到tab切换的模块,那么,实现他的方法也有很多中,下面是一款简单,容易理解的react tab切换方法. 通过设置state中的current 属性去控制tab 和 ...

  3. jquery实现简单的Tab切换菜单

    实现tab切换的主要html代码: <div class="container"> <ul class="tabs"> <li c ...

  4. Flutter——TabBar组件(顶部Tab切换组件)

    TabBar组件的常用属性: 属性 描述 tabs 显示的标签内容,一般使用 Tab 对象,也可以是其他的Widget  controller TabController 对象 isScrollabl ...

  5. DIV+CSS 样式简单布局Tab 切换

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  6. jQuery实现简单的tab切换

    html: <section>   <nav id="nav">     <a class="on">tab1</a& ...

  7. vue3 封装简单的 tabs 切换组件

    背景:公司项目要求全部换成 vue3 ,而且也没有应用像 element-ui 一类的UI组件,用到的公共组件都是根据项目需求封装的,下面是使用vue3实现简单的tabs组件,我只是把代码分享出来,实 ...

  8. 基于Vue开发的tab切换组件

    github地址:https://github.com/MengFangui/VueTabSwitch 1.index.html <!DOCTYPE html> <html lang ...

  9. 【angular】angular实现简单的tab切换

    html: <div class="list-group" ng-repeat="tab in menuList"> <a href=&quo ...

随机推荐

  1. php命令行生成与读取配置文件

    接着之前的文章:php根据命令行参数生成配置文件 ghostinit.php <?php class ghostinit{ static $v = 'ghost version is 1.1'; ...

  2. Django Rest framework 之 视图

    RESTful 规范 django rest framework 之 认证(一) django rest framework 之 权限(二) django rest framework 之 节流(三) ...

  3. AsnycLocal与ThreadLocal

    AsnycLocal与ThreadLocal AsnyncLocal与ThreadLocal都是存储线程上下文的变量,但是,在实际使用过程中两者又有区别主要的表现在: AsyncLocal变量可以在父 ...

  4. python中read()、readline()、readlines()函数

    python文件读read().readline().readlines()对比   目录 一.read方法 二.readline方法 三.readlines方法 正文 读取文件的三个方法:read( ...

  5. 【代码笔记】iOS-动画的跳转

    一,工程图. 二,代码. //点击任何处跳转到页面 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { CATransi ...

  6. 过滤器(Filter)与拦截器(Interceptor)的区别

    1 .拦截器是基于java的反射机制的,而过滤器是基于函数回调. 2 .拦截器不依赖与servlet容器,过滤器依赖与servlet容器. 3 .拦截器只能对action请求起作用,而过滤器则可以对几 ...

  7. gis cad导出弧段在arcmap下 不准问题

    我发现cad 的图形导出到arcmap下会出现各种各样的丢失问题,特别是cad的弧段在arcmap下会弯曲(弧度指向另外一边). 那么应该怎么解决这个问题呢?后来想到FME可以高效的还原cad的图形, ...

  8. Python笔记(九):字符串操作

    (一)    字符串 单引号.双引号.三重引号都可以作为字符串的开始和结束,三重引号可以直接输入多行字符串.三重引号可能一般是用来写多行注释. (二)    r和\ r使字符串成为原始字符串,忽略所有 ...

  9. 阿里云rds实例恢复到本地

    摘要: 前提: 1,阿里云数据库备份实例,恢复数据的时候需要将数据恢复到本地数据库,是不能直接恢复到RDS上的. 2,需要在本地服务器上下载一个数据库,尽量和RDS数据库版本保持一致.(我现在用的是5 ...

  10. 控制台输出 mybatis 中的sql语句

    控制台输出 mybatis 中的sql语句 在 log4j.xml 文件中 增加如下配置 <!-- mybatis 输出的sql,DEBUG级别 --> <logger name=& ...