jQuery插件實(shí)現(xiàn)非常實(shí)用的tab欄切換功能【案例】
本文實(shí)例講述了jQuery插件實(shí)現(xiàn)tab欄切換功能。分享給大家供大家參考,具體如下:
效果:

核心代碼:自己寫了一個(gè)方法,需要用的時(shí)候直接調(diào)用就可以了.
方法如下:
(function ($) {
//給$的fn添加方法
$.fn.tabs=function ( options ) {
/* {
tabHeads:'tab-menu>li',
tabHeadsClass:'active',
tabBodys:'tab-main>div',
tabBodysClass:'selected'
}
*/
/**
* @param options 對(duì)象
* @param options.tabHeads:上面的li標(biāo)簽
* @param options.tabHeadsClass:li標(biāo)簽需要添加的類名
* @param options.tabBodys:下面四個(gè)內(nèi)容盒子
* @param options.tabBodysClass:下面四個(gè)內(nèi)容盒子需要添加的類名
*/
//將fn這個(gè)對(duì)象存起來,代碼結(jié)束的時(shí)候讓它返回
var $bigDiv=this;
//1.給頁簽們添加點(diǎn)擊事件
$(options.tabHeads).on('click',function ( ) {
//2.給被點(diǎn)擊的li標(biāo)簽添加類,給其它兄弟標(biāo)簽移除這個(gè)類
$(this).addClass(options.tabHeadsClass).siblings().removeClass(options.tabHeadsClass);
//3.獲取當(dāng)前點(diǎn)擊的標(biāo)簽的索引
var idx=$(this).index();
//4.從下面div里面找到和idx相同的索引,給它添加類,其它兄弟頁面移除這個(gè)類
$(options.tabBodys).eq(idx).addClass(options.tabBodysClass).siblings().removeClass(options.tabBodysClass)
})
return $bigDiv;
}
}(jQuery))
代碼結(jié)構(gòu):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>www.dbjr.com.cn jQuery tab切換</title>
<style>
*{
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
.tab {
width: 400px;
height: 50px;
background: #ccc;
margin: 100px auto ;
}
.tab-body {
width: 400px;
height: 398px;
border: 1px solid #ccc;
}
.tab .item {
display: none;
padding-left: 30px;
}
.tab-head li {
float: left;
width: 100px;
height: 50px;
text-align: center;
line-height: 50px;
cursor: pointer;
}
/*讓下方的內(nèi)容顯示*/
.item.selected{
display: block;
}
/*讓上方li標(biāo)簽改變顏色*/
.active{
background-color: hotpink;
}
</style>
</head>
<body>
<div class="tab">
<ul class="tab-head">
<li class="active">頁簽1</li>
<li >頁簽2</li>
<li >頁簽3</li>
<li >頁簽4</li>
</ul>
<div class="tab-body">
<div class="item selected">
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
</div>
<div class="item">
<p>內(nèi)容2</p>
<p>內(nèi)容2</p>
<p>內(nèi)容2</p>
<p>內(nèi)容2</p>
</div>
<div class="item">
<p>內(nèi)容3</p>
<p>內(nèi)容3</p>
<p>內(nèi)容3</p>
<p>內(nèi)容3</p>
</div>
<div class="item">
<p>內(nèi)容4</p>
<p>內(nèi)容4</p>
<p>內(nèi)容4</p>
<p>內(nèi)容4</p>
</div>
</div>
</div>
<!--jq代碼實(shí)現(xiàn)過程-->
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<!--//引入自己寫的方法-->
<script src="jQuery-tabs.js"></script>
<script>
$ ( function () {
//先自己寫一個(gè)jQuery-tabs方法(相當(dāng)于js插件)
// 調(diào)用自己寫的方法
$('#wrapper').tabs(
{
tabHeads:'.tab-head>li',
tabHeadsClass:'active',
tabBodys:'.tab-body>div',
tabBodysClass:'selected'
}
)
} )
</script>
</body>
</html>
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼運(yùn)行效果。
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery頁面元素操作技巧匯總》、《jQuery常見事件用法與技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
相關(guān)文章
jquery實(shí)現(xiàn)圖片漸變切換兼容ie6/Chrome/Firefox
jquery代碼實(shí)現(xiàn)圖片漸變切換同時(shí)兼容ie6、Chrome、Firefox,想學(xué)習(xí)的朋友可以測(cè)試下,希望對(duì)大家有所幫助2013-08-08
jQuery+ajax實(shí)現(xiàn)滾動(dòng)到頁面底部自動(dòng)加載圖文列表效果(類似圖片懶加載)
這篇文章主要介紹了jQuery+ajax實(shí)現(xiàn)滾動(dòng)到頁面底部自動(dòng)加載圖文列表效果,模擬圖片懶加載功能,涉及jQuery的ajax與asp.net交互動(dòng)態(tài)顯示頁面內(nèi)容的相關(guān)技巧,需要的朋友可以參考下2016-06-06
jQuery中對(duì)節(jié)點(diǎn)進(jìn)行操作的相關(guān)介紹
本篇文章小編將為大家介紹,在jQuery中對(duì)節(jié)點(diǎn)進(jìn)行操作的解決辦法,有需要的朋友可以參考一下2013-04-04
jquery遍歷篩選數(shù)組的幾種方法和遍歷解析json對(duì)象
本文為大家介紹下jquery遍歷篩選數(shù)組的幾種方法和遍歷解析json對(duì)象的具體實(shí)現(xiàn),感興趣的朋友不要錯(cuò)過2013-12-12
jQuery實(shí)現(xiàn)監(jiān)聽下拉框選中內(nèi)容發(fā)生改變操作示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)監(jiān)聽下拉框選中內(nèi)容發(fā)生改變操作,結(jié)合實(shí)例形式分析了jQuery針對(duì)select選中觸發(fā)change事件相關(guān)操作技巧,需要的朋友可以參考下2018-07-07

