JavaScript插件Tab選項(xiàng)卡效果
閑來(lái)無(wú)事,研究了一下JavaScript插件的寫(xiě)法,今天就將自己寫(xiě)的一個(gè)小插件記錄下來(lái)。
本文介紹了此款插件的基本用法,實(shí)現(xiàn)的功能以及代碼。
首先,來(lái)看看最終效果:

這是一款普通的Tab選項(xiàng)卡插件,下面來(lái)講講它實(shí)現(xiàn)了哪些功能:
1、支持不同鼠標(biāo)事件觸發(fā)選項(xiàng)卡切換效果;
2、支持不同切換效果的配置,例如淡入淡出/直接切換;
3、支持默認(rèn)展示第幾個(gè)選項(xiàng)卡的配置;
4、支持選項(xiàng)卡的自動(dòng)切換效果。
例子很簡(jiǎn)單,需要用到的知識(shí)包括:
1、html、css的基礎(chǔ)知識(shí);
2、對(duì)this,prototype,new等關(guān)鍵詞的理解。
簡(jiǎn)而言之,就是通過(guò)參數(shù)配置的形式來(lái)完成不同效果的展示。
下面先看一看如何使用:
1、$(".js-tab").etab();
2、$(".js-tab").etab({
triggerType: "click",
effect: "fade",
invoke: 2,
auto: 3000
});
3、Tab.init($(".js-tab"));
本插件支持幾種不同的初始化方式,代碼很簡(jiǎn)單,類(lèi)似于BootStrap插件的使用方法。下面奉上完整的代碼:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tab選項(xiàng)卡</title>
<link href="tab.css" rel="stylesheet">
<style>
* {
margin:0;
padding:0;
}
body {
background-color: #323232;
font-size:12px;
font-family:微軟雅黑;
padding:100px;
}
ul, li {
list-style-type: none;
}
</style>
<script src="../lib/jquery-1.11.3.js"></script>
<script type="text/javascript" src="tab.js"></script>
</head>
<body>
<div class="js-tab tab">
<ul class="tab-nav">
<li class="active"><a href="#">新聞</a> </li>
<li><a href="#">電影</a> </li>
<li><a href="#">娛樂(lè)</a> </li>
<li><a href="#">科技</a> </li>
</ul>
<div class="content-wrap">
<div class="content-item current">
<h3>新聞</h3>
</div>
<div class="content-item">
<h3>電影</h3>
</div>
<div class="content-item">
<h3>娛樂(lè)</h3>
</div>
<div class="content-item">
<h3>科技</h3>
</div>
</div>
</div>
<script>
$(function() {
// Tab.init($(".js-tab"));
$(".js-tab").etab({
triggerType: "click",
effect: "fade",
invoke: 2,
auto: 3000
});
$(".js-tab").etab();
});
</script>
</body>
</html>
.tab {
width: 300px;
}
.tab .tab-nav {
height: 30px;
}
.tab .tab-nav li {
float: left;
margin-right:5px;
background-color:#767676;
border-radius:3px 3px 0 0;
}
.tab .tab-nav li a{
display:block;
height:30px;
padding:0 20px;
color: white;
line-height:30px;
text-decoration: none;
}
.tab .tab-nav .active {
background-color: #fff;
}
.tab .tab-nav .active a{
color: #777;
}
.tab .content-wrap{
background-color: white;
padding:5px;
height:200px
}
.tab .content-wrap .content-item {
position:absolute;
height: 200px;
display: none;
}
.tab .content-wrap .current {
height: 200px;
display: block;
}
最后將插件代碼列出來(lái),在代碼里面已經(jīng)寫(xiě)了很詳細(xì)的注釋?zhuān)?/p>
/**
* Created by Wu.lin on 2017/11/12.
*/
(function($){
var Tab = function(tab, _params) {
var _this = this;
//保存單個(gè)Tab組件
this.tab = tab;
this.params = _params;
//默認(rèn)配置參數(shù)
this.config = {
//用來(lái)定義鼠標(biāo)的出發(fā)類(lèi)型 "click"/mouseover
"triggerType": "mouseover",
//用來(lái)定義內(nèi)容切換效果,直接切換/淡入淡出
"effect": "default",
//默認(rèn)展示第幾個(gè)Tab
"invoke": "1",
//用來(lái)定義Tab是否自動(dòng)切換,當(dāng)指定了事件間隔,就表示自動(dòng)切換,并指定了切換間隔
"auto": false
};
//如果配置參數(shù)存在,就擴(kuò)展默認(rèn)的配置參數(shù)
if(this.params){
$.extend(this.config, this.params);
}
//保存Tab標(biāo)簽列表,以及對(duì)應(yīng)的內(nèi)容列表
this.tabItem = this.tab.find("ul.tab-nav li");
this.contentItem = this.tab.find("div.content-wrap .content-item");
//保存配置參數(shù)
var config = this.config;
if(config.triggerType === "click") {
this.tabItem.bind(config.triggerType, function() {
_this.invoke($(this));
});
} else {
this.tabItem.mouseover(function(){
_this.invoke($(this));
});
}
//自動(dòng)切換功能
if(config.auto) {
this.timmer = null;
//計(jì)數(shù)器
this.loop = 0;
this.autoPlay();
this.tab.hover(function() {
window.clearInterval(_this.timmer);
}, function() {
_this.autoPlay();
});
}
//設(shè)置默認(rèn)顯示第幾個(gè)Tab
if(config.invoke > 1) {
this.invoke(this.tabItem.eq(config.invoke - 1));
}
};
Tab.prototype = {
//事件驅(qū)動(dòng)函數(shù)
invoke: function(currentTab) {
/**
* 1、執(zhí)行Tab選中狀態(tài),當(dāng)前選中Tab加上Active,
* 2、切換對(duì)應(yīng)Tab內(nèi)容,根據(jù)配置參數(shù)effect參數(shù)default|fade
*/
var index = currentTab.index();
var conItem = this.contentItem;
//Tab切換
currentTab.addClass("active").siblings().removeClass("active");
//內(nèi)容區(qū)域切換
var effect = this.config.effect;
if(effect === "fade") {
conItem.eq(index).fadeIn().siblings().fadeOut();
} else {
conItem.eq(index).addClass("current").siblings().removeClass("current");
}
//注意,如果配置了自動(dòng)切換,記得把當(dāng)前的loop值設(shè)置為當(dāng)前的Tab的index
if(this.config.auto) {
this.loop = index;
}
},
//自動(dòng)間隔切換
autoPlay: function() {
var _this_ = this,
tabItems = this.tabItem, //臨時(shí)保存Tab列表
tabLength = tabItems.size(),
config = this.config;
this.timmer = window.setInterval(function() {
_this_.loop++;
if(_this_.loop >= tabLength) {
_this_.loop = 0;
}
tabItems.eq(_this_.loop).trigger(config.triggerType);
}, config.auto);
}
};
Tab.init = function(tabs) {
var _this_ = this;
tabs.each(function() {
new _this_($(this));
});
// var tab = new Tab($(".js-tab").eq(0));
};
//注冊(cè)成JQuery方法
$.fn.extend({
etab: function(_param) {
this.each(function () {
new Tab($(this), _param);
});
return this;
}
});
window.Tab = Tab;
})(jQuery);
如此看來(lái),是不是很簡(jiǎn)單,一起來(lái)動(dòng)手試試吧!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaScript tab選項(xiàng)卡插件實(shí)例代碼
- jquery插件tytabs.jquery.min.js實(shí)現(xiàn)漸變TAB選項(xiàng)卡效果
- js實(shí)現(xiàn)簡(jiǎn)單選項(xiàng)卡功能
- js實(shí)現(xiàn)簡(jiǎn)單的可切換選項(xiàng)卡效果
- js選項(xiàng)卡的實(shí)現(xiàn)方法
- javascript實(shí)現(xiàn)tabs選項(xiàng)卡切換效果(自寫(xiě)原生js)
- JS實(shí)現(xiàn)選項(xiàng)卡實(shí)例詳解
- 使用vue.js寫(xiě)一個(gè)tab選項(xiàng)卡效果
- Vue.js組件tabs實(shí)現(xiàn)選項(xiàng)卡切換效果
- 原生js實(shí)現(xiàn)tab選項(xiàng)卡切換
- js實(shí)現(xiàn)tab選項(xiàng)卡函數(shù)代碼
- Vue.js組件tab實(shí)現(xiàn)選項(xiàng)卡切換
- JS實(shí)現(xiàn)選項(xiàng)卡插件的兩種寫(xiě)法(jQuery和class)
相關(guān)文章
a標(biāo)簽click和href執(zhí)行順序探討
這篇文章主要介紹了a標(biāo)簽click和href執(zhí)行順序,需要的朋友可以參考下2014-06-06
javascript之querySelector和querySelectorAll使用說(shuō)明
其實(shí)關(guān)于querySelector和querySelectorAll的介紹說(shuō)明很多,在此主要是做個(gè)記錄2011-10-10
小程序云開(kāi)發(fā)如何實(shí)現(xiàn)圖片上傳及發(fā)表文字
這篇文章主要為大家詳細(xì)介紹了小程序云開(kāi)發(fā)教程,如何實(shí)現(xiàn)圖片上傳及發(fā)表文字,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
Javascript實(shí)現(xiàn)前端簡(jiǎn)單的路由實(shí)例
本文將使用javascript實(shí)現(xiàn)一個(gè)極其簡(jiǎn)單的路由實(shí)例。WEB開(kāi)發(fā)中路由概念并不陌生,我們接觸到的有前端路由和后端路由。后端路由在很多框架中是一個(gè)重要的模塊,同樣前端路由在單頁(yè)面應(yīng)用也很常見(jiàn),它使得前端頁(yè)面體驗(yàn)更流暢。2016-09-09
javascript去除字符串中所有標(biāo)點(diǎn)符號(hào)和提取純文本的正則
這篇文章主要介紹了javascript去除字符串中所有標(biāo)點(diǎn)符號(hào)和提取純文本的正則,需要的朋友可以參考下2014-06-06

