编写tab切换插件
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切换插件的更多相关文章
- iTabs Tab切换插件
最近项目中使用到Tab切换,切换的页面不变,内容发生变化,随手写了份简单的插件,附带源码.先看样子: 本人也考虑到是否使用jquery ui tab,但是还是热衷于自己写一份,首先好处之一是易于培训, ...
- 一个小的tab切换插件
1//使用 var t1=new Tab({ etype:'onmou',//默认点击触发,如果事件写错了,当作单击 autoplay:2000,//有时间值(按照事件自动播放)和false(不自动播 ...
- jQuery的DOM操作实例(1)——选项卡&&Tab切换
一.原生JavaScript编写tab切换 二.jQuery编写tab切换 在用jQuery编写选项卡过程中,重要的事搞清楚 .eq() 和 .index() 的使用方法. .eq()是jQuery遍 ...
- tab切换插件开发
我开发的tab切换插件,基于jquery库,实现tab标签页的切换.插件的名称为jquery.tabSwitch.js. 插件实现代码如下: ; (function ($) { $.fn.tabSwi ...
- 自己编写jQuery插件之Tab切换
自己编写jQuery插件之 Tabs切换 jquery ui 带有Tabs切换插件,但其css样式太难维护,引用的东西太多,因此就自己写了个. 起初我Html代码架子是这样的: <div cla ...
- swiper插件遇上tab切换
当swiper插件遇到tab切换,即display的显示与否属性时,失效,方法如下: <script language="javascript"> var mySwip ...
- 前端tab切换 和 validatejs表单验证插件
一.tab切换 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- jquery TAB切换小插件
//tab切换 ;(function($, window, document, undefined) { $.fn.tab = function(options) { var defaults = { ...
- 解决微信小程序的wx-charts插件tab切换时的显示会出现位置移动问题-tab切换时,图表显示错乱-实现滑动tab
解决Echarts在微信小程序tab切换时的显示会出现位置移动问题 tab切换时,图表显示错乱 <canvas class="kcanvas" canvas-id=" ...
随机推荐
- P4559 [JSOI2018]列队
\(\color{#0066ff}{ 题目描述 }\) 作为一名大学生,九条可怜在去年参加了她人生中的最后一次军训. 军训中的一个重要项目是练习列队,为了训练学生,教官给每一个学生分配了一个休息位置. ...
- 6.House Robber(简单版抢银行)
Level: Easy 题目描述: You are a professional robber planning to rob houses along a street. Each house ...
- eclipse字体
- Experimental Educational Round: VolBIT Formulas Blitz N
Description The Department of economic development of IT City created a model of city development ti ...
- Go语言基础之9--指针类型详解
一. 变量和内存地址 每个变量都有内存地址,可以说通过变量来操作对应大小的内存 注意:通过&符号可以获取变量的内存地址 通过下面例子来理解下: 实例1-1 package main impor ...
- thymeleaf总结
thymeleaf 基本功能是将 th:xxx的内容替换html标签的内容 原标签的内容会被替换掉,原内容只是前端用来显示demo的 和freemarker, velocity的重要区别是,它们的自定 ...
- hutool http+天气预报
中国天气接口:http://www.weather.com.cn/data/sk/地址.html,只显示当天. sojson接口:http://t.weather.sojson.com/api/wea ...
- Angular JS 1.X 接口拿不到 http post 请求的数据
app上加上配置相关的代码即可 var myApp = angular.module('myApp',[]); myApp.config(function($httpProvider){ $httpP ...
- Sublime text中文乱码解决办法
ConvertToUTF8 安装这个插件可以解决编码混乱问题 首先必须先配一下Sublime text ,安装 Package Control 1. 用Sublimt text 打开任意一个文件,C ...
- scp —— 服务器之间互传文件
scp 可以在 2个 linux 主机间复制文件: 从 本地 复制到 远程 * 复制文件: 举例子: scp /home/space/music/.mp3 root@192.168.0.1 ...