html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
list-style: none;
font-size: 14px;
font-family: 'Microsoft yahei';
}
.tab {
width: 300px;
height: 400px;
margin: 30px 0 0 30px;
position: relative;
}
.tab_nav {
width: 300px;
height: 43px;
position: absolute;
left: 0;
top: 0;
border: 1px solid #ccc;
border-bottom: none;
}
.tab_cont {
width: 302px;
height: 356px;
position: absolute;
left: 0;
top: 43px;
}
.tab_nav > li {
float: left;
width: 100px;
height: 40px;
line-height: 40px;
text-align: center;
border-top: 3px solid green;
cursor: pointer;
}
.tab_nav > li.active {
border-top: 3px solid orange;
background: #f7f7f7;
}
.tab_cont > li {
width: 300px;
height: 356px;
border: 1px solid #ccc;
display: none;
}
.tab_cont > li.active {
display: block;
}
</style>
</head>
<body> <div class="tab">
<ul class="tab_nav">
<li class='tab_nav_item active'>导航一</li>
<li class="tab_nav_item">导航二</li>
<li class="tab_nav_item">导航三</li>
</ul>
<ul class="tab_cont">
<li class="tab_cont_item active">内容一</li>
<li class="tab_cont_item">内容二</li>
<li class="tab_cont_item">内容三</li>
</ul>
</div> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Earum, illo officia quidem recusandae nihil consectetur sunt tempore tenetur voluptate atque quasi doloremque ratione eaque, sequi nam ducimus, eligendi deleniti modi.</p> <script src="jquery.min.js"></script>
<script src="jq.fn.tabcc.js"></script>
<script> // 参数以对象的形式传入
// 调用方式:$('.tab').tabcc({});
$('.tab').tabcc({
'navItem': '.tab_nav_item',
'contItem': '.tab_cont_item'
}); </script> </body>
</html>

js:

;(function ($) {
// 1.创建一个构造函数,传入要绑定的元素和参数的对象,初始化defaults参数默认值
var Tab = function (ele, options) {
this.$element = ele;
this.defaults = {
'navItem': '',
'contItem': ''
};
this.options = $.extend({}, this.defaults, options);
}; // 2.暴露出原型方法,对元素进行操作
Tab.prototype = {
_init: function () {
var $this = this.$element;
var $navItem = this.options.navItem;
var $contItem = this.options.contItem; $this.find($navItem).on('click', function () {
//console.log($(this).index());
var _index = $(this).index();
//console.log(_index);
$this.find($navItem).each(function () {
$(this).removeClass('active');
});
$(this).addClass('active'); $this.find($contItem).each(function () {
$(this).removeClass('active');
});
$this.find($contItem).eq(_index).addClass('active');
});
}
}; // 3.把方法放在插件扩展里,形成插件,方便调用
$.fn.tabcc = function (options) {
// 初始化构造函数对象(实例化对象)
var tab = new Tab(this, options);
tab._init();
};
})(jQuery);

添加一个fade效果参数:

;(function ($, win, doc, undefined) {
// 1.创建一个构造函数,传入要绑定的元素和参数的对象,初始化defaults参数默认值
var Tab = function (ele, options) {
this.$element = ele;
this.defaults = {
navItem: '',
contItem: '',
eventType: 'click',
animateSwitch: undefined // fade|slide|toTop
};
this.options = $.extend({}, this.defaults, options);
}; // 2.暴露出原型方法,对元素进行操作
Tab.prototype = {
_init: function () {
var $this = this.$element;
var $navItem = this.options.navItem;
var $contItem = this.options.contItem;
var $eventType = this.options.eventType;
var $animateSwitch = this.options.animateSwitch; // 改变事件,mouseover或者click,让用户自己传入
$this.find($navItem).on($eventType, function () {
//console.log($(this).index());
var _index = $(this).index();
//console.log(_index);
$this.find($navItem).each(function () {
$(this).removeClass('active');
}); $this.find($contItem).each(function () {
$(this).removeClass('active');
}); if($animateSwitch == undefined) {
$(this).addClass('active');
$this.find($contItem).eq(_index).addClass('active'); }else if($animateSwitch == 'fade') {
$this.find($contItem).each(function () {
$(this).removeClass('active').hide();
}); $this.find($contItem).eq(_index).fadeIn(); $(this).addClass('active'); }
});
}
}; // 3.把方法放在插件扩展里,形成插件,方便调用
$.fn.tabcc = function (options) {
// 初始化构造函数对象(实例化对象)
var tab = new Tab(this, options);
tab._init();
};
})(jQuery, window, document);

编写tab切换插件的更多相关文章

  1. iTabs Tab切换插件

    最近项目中使用到Tab切换,切换的页面不变,内容发生变化,随手写了份简单的插件,附带源码.先看样子: 本人也考虑到是否使用jquery ui tab,但是还是热衷于自己写一份,首先好处之一是易于培训, ...

  2. 一个小的tab切换插件

    1//使用 var t1=new Tab({ etype:'onmou',//默认点击触发,如果事件写错了,当作单击 autoplay:2000,//有时间值(按照事件自动播放)和false(不自动播 ...

  3. jQuery的DOM操作实例(1)——选项卡&&Tab切换

    一.原生JavaScript编写tab切换 二.jQuery编写tab切换 在用jQuery编写选项卡过程中,重要的事搞清楚 .eq() 和 .index() 的使用方法. .eq()是jQuery遍 ...

  4. tab切换插件开发

    我开发的tab切换插件,基于jquery库,实现tab标签页的切换.插件的名称为jquery.tabSwitch.js. 插件实现代码如下: ; (function ($) { $.fn.tabSwi ...

  5. 自己编写jQuery插件之Tab切换

    自己编写jQuery插件之 Tabs切换 jquery ui 带有Tabs切换插件,但其css样式太难维护,引用的东西太多,因此就自己写了个. 起初我Html代码架子是这样的: <div cla ...

  6. swiper插件遇上tab切换

    当swiper插件遇到tab切换,即display的显示与否属性时,失效,方法如下: <script language="javascript"> var mySwip ...

  7. 前端tab切换 和 validatejs表单验证插件

    一.tab切换 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  8. jquery TAB切换小插件

    //tab切换 ;(function($, window, document, undefined) { $.fn.tab = function(options) { var defaults = { ...

  9. 解决微信小程序的wx-charts插件tab切换时的显示会出现位置移动问题-tab切换时,图表显示错乱-实现滑动tab

    解决Echarts在微信小程序tab切换时的显示会出现位置移动问题 tab切换时,图表显示错乱 <canvas class="kcanvas" canvas-id=" ...

随机推荐

  1. luogu3704 [SDOI2017]数字表格(莫比乌斯反演)

    link 设\(f_0=0,f_1=1,f_n=f_{n-1}+f_{n-2}(n\ge 2)\) 求\(\prod_{i=1}^n\prod_{j=1}^mf_{\gcd(i,j)}\),多组询问, ...

  2. Fxx and game hdu 5945 单调队列dp

    dfs你怕是要爆炸 考虑dp; 很容易想到 dp[ i ] 表示到 i 时的最少转移步数: 那么: dp[ i ]= min( dp[ i ],dp[ i-j ]+1 ); 其中 i-t<=j& ...

  3. kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  4. [WebShow系列] 评委打分端现场操作方法

    前期准备: 在活动现场,组委会为每一个评委准备了打分相关东西: A.一个移动打分设备(平板电脑,或评委自己手机,或电脑也行); B.(可选)纸质的打分清单和笔.此清单中有打分细则,上场选手清单及打分处 ...

  5. linux中tomcat内存溢出

    刚开始测试服务器与线上后台都不能上传10分钟以上的视频,后来只要是视频就不能上传,进入服务器查日志得到如下错误: Caused by: java.lang.OutOfMemoryError: Java ...

  6. spring读取配置文件,且获取bean实例

    import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.Xm ...

  7. pytorch构建优化器

    这是莫凡python学习笔记. 1.构造数据,可以可视化看看数据样子 import torch import torch.utils.data as Data import torch.nn.func ...

  8. maven 过滤webapp下的文件

    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-p ...

  9. Linux防火墙详解

    1.Linux防火墙基础 作为隔离内外网.过滤非法数据的有力屏障,防火墙通常按实现环境的不同分为硬件防火墙和软件防火墙.硬件防火墙是功能专一的硬件设备,具有比较全面的功能,其工作效率较高,但是加个昂贵 ...

  10. JavaScript trim 实现去除字符串首尾指定字符的简单方法

    String.prototype.trim = function (char, type) { if (char) { if (type == 'left') { return this.replac ...