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

代码如下:

/**
* 简单的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. 数据库导入导出expdp,impdp

    数据库操作 (1)数据库导入导出expdp,impdp 在导入导出数据库的时候,经常会用到exp和imp,在数据量小的情况下可以随意使用,但是当数据量大,表中数据有百万,千万条的时候,就要等好久好久好 ...

  2. ThinkPHP5事务回滚

    使用事务处理的话,需要数据库引擎支持事务处理.比如 MySQL 的 MyISAM 不支持事务处理,需要使用 InnoDB 引擎. 使用 transaction 方法操作数据库事务,当发生异常会自动回滚 ...

  3. 学习笔记(4)——实验室集群管理结点IP配置

    经过验证,集群管理结点mgt的IP配置应为如下所示: [root@mgt zmq]# ifconfig//外部网卡 eth0 Link encap:Ethernet HWaddr 5C:F3:FC:E ...

  4. 关于ArrayAdapter的getCount()的方法会造成空指针异常的分析

    在继承了ArrayAdapter重新getCount()的方法后,比如 public int getCount() { return 3; } 若在活动中调用setAdapter()的方法前,List ...

  5. Expo大作战(十九)--expo打包后,发布分用程序到商店的注意事项

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...

  6. UWP开发细节记录:判断文件类型

    StorageFile.ContentType 属性,是 string 类型,用来表示文件内容的 MIME 类型.例如,音乐文件可能有 "audio/mpeg" MIME 类型.( ...

  7. 如何在 Azure 中标记 Windows 虚拟机

    本文介绍在 Azure 中通过 Azure 资源管理器标记 Windows 虚拟机的不同方式.标记是用户定义的键/值对,可直接放置在资源或资源组中.针对每个资源和资源组,Azure 当前支持最多 15 ...

  8. Java 如何启用 ARM 虚拟机诊断

    现象描述 如何通过 Java 语言实现在创建 ARM 虚拟机时开启诊断,并配置相关指标.   实现思路 调研最高版本的 JAVA SDK(1.1.0)源码发现,SDK 层面并未提供任启动诊断和配置诊断 ...

  9. Oracle EBS GL 会计科目报错 GL_ACCESS_SET_LEDGERS

    1.会计科目设置后,总账中找不到对应账簿                                           2.原因是新版本系统物化视图有问题,参照metalink解决方案得知原路径 ...

  10. SQL Server 合并复制如何把备份的发布端或订阅端BAK文件还原为数据库

    SQL Server的合并复制,是可以备份发布端和订阅端数据库为BAK文件的,但是问题是合并复制在数据库中自动创建的系统表.触发器.表中的RowGuid列等也会被一起备份. 这里我们举个例子,下面图中 ...