jQuery實(shí)現(xiàn)Tab選項(xiàng)卡切換效果簡(jiǎn)單演示
本文實(shí)例針對(duì)jQuery實(shí)現(xiàn)Tab選項(xiàng)卡切換效果進(jìn)行了簡(jiǎn)單演示,完全是自己的思考實(shí)現(xiàn)過程,分享給大家供大家參考。具體如下:
起初我Html代碼架子是這樣的:
<div class="tabs"> <ul> <li class="acss" data-box="#panel-1">標(biāo)簽1</li> <li class="bcss" data-box="#panel-2">標(biāo)簽2</li> <li class="bcss" data-box="#panel-3">標(biāo)簽3</li> </ul> <div id="panel-1">內(nèi)容111111</div> <div id="panel-2" style="display:none;">內(nèi)容222222</div> <div id="panel-3" style="display:none;">內(nèi)容333333</div> </div>
后來換成了下面這個(gè):
<dl class="tabs"> <dt> <a class="acss" href="#panel-1">標(biāo)簽1</a> <a class="bcss" href="#panel-2">標(biāo)簽2</a> <a class="bcss" href="#panel-3">標(biāo)簽3</a> </dt> <dd id="panel-1">內(nèi)容1</dd> <dd id="panel-2" style="display:none;">內(nèi)容2</dd> <dd id="panel-3" style="display:none;">內(nèi)容3</dd> </dl>
之所以換成這個(gè),是因?yàn)槲矣X得 dl dt dd 在頁面布局中用的比 div ul li 要少,這樣可以做到更好的隔離性。我們用js操作dl dt dd 對(duì)象,就會(huì)更少的影響到頁面內(nèi)其它元素,還有就是不用在li標(biāo)簽中自定義data-box屬性,更符合頁面書寫標(biāo)準(zhǔn)。而且這個(gè)結(jié)構(gòu)的整體感覺也比上面那個(gè)好。
插件的實(shí)現(xiàn)代碼如下:
(function ($) { $.fn.Tabs = function (options) { //默認(rèn)參數(shù)設(shè)置 var settings = { beforeCss: "bcss", //激活前樣式名 afterCss: "acss", //激活后樣式名 model: "mouseover" //切換方式("mouseover"或者"click") }; //不為空,則合并參數(shù) if (options) $.extend(settings, options); //獲取a標(biāo)簽集合 var arr_a = $("> dt > a", this); //給a標(biāo)簽分別綁定事件 arr_a.each(function () { $(this).bind(settings.model, function (event) { //去除a標(biāo)簽的錨點(diǎn)跳轉(zhuǎn) event.preventDefault(); //樣式控制 $(this).removeClass().addClass(settings.afterCss) .siblings("a").removeClass().addClass(settings.beforeCss); //隱藏與顯示控制 var dd_id = $(this).attr("href"); $(dd_id).show().siblings("dd").hide(); }); }); //遵循鏈?zhǔn)皆瓌t return this.each(function () { }); }; })(jQuery);
之所以說是輕量級(jí),是因?yàn)榇a量真的很少,也很簡(jiǎn)單。加了注釋相信大家都可以看懂。
其中settings中的model是用來控制切換方式的:
- 當(dāng)為"click"時(shí),點(diǎn)擊實(shí)現(xiàn)切換;
- 當(dāng)為"mouseover"時(shí),鼠標(biāo)滑入實(shí)現(xiàn)切換。
開始時(shí)是想用hover來實(shí)現(xiàn)鼠標(biāo)滑入切換的,結(jié)果發(fā)現(xiàn),hover不支持bind綁定。因?yàn)閔over是jquery通過封裝 mouseover事件 的產(chǎn)物,它并不是一個(gè)正宗的事件,因此無法綁定。
下面給出個(gè)DEMO:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <style type="text/css"> * { margin: 0; padding: 0; } .tabs { width: 504px; margin: 50px auto; } .acss,.bcss { text-decoration:none; line-height: 35px; font-size: 14px; padding:8px 15px; } .bcss { background-color: #D4D4D4; border-bottom:1px solid white; } .acss { background-color: orange; border-bottom:1px solid orange; } .tabs dd { width: 500px; height: 300px; border: 1px solid orange; text-align: center; line-height: 300px; } </style> </head> <body> <dl class="tabs" id="tabs1"> <dt> <a class="acss" href="#panel-1">標(biāo)簽1</a> <a class="bcss" href="#panel-2">標(biāo)簽2</a> <a class="bcss" href="#panel-3">標(biāo)簽3</a> </dt> <dd id="panel-1"><h1>鼠標(biāo)滑入切換</h1></dd> <dd id="panel-2" style="display:none;">內(nèi)容2</dd> <dd id="panel-3" style="display:none;">內(nèi)容3</dd> </dl> <dl class="tabs" id="tabs2"> <dt> <a class="acss" href="#panel-4">標(biāo)簽1</a><!--默認(rèn)第一個(gè)激活--> <a class="bcss" href="#panel-5">標(biāo)簽2</a> <a class="bcss" href="#panel-6">標(biāo)簽3</a> </dt> <dd id="panel-4"><h1>鼠標(biāo)點(diǎn)擊切換</h1></dd><!--默認(rèn)第一個(gè)顯示--> <dd id="panel-5" style="display:none;">內(nèi)容2</dd> <dd id="panel-6" style="display:none;">內(nèi)容3</dd> </dl> <script src="../js/jquery-1.4.1.min.js" type="text/javascript"></script> <script src="../js/jquery.similar.Tabs.js" type="text/javascript"></script> <script type="text/javascript"> $("#tabs1").Tabs(); //默認(rèn)鼠標(biāo)滑入切換 $("#tabs2").Tabs({model:"click"}); //設(shè)置為點(diǎn)擊切換 </script> </body> </html>
效果圖如下:
希望本文所述對(duì)大家學(xué)習(xí)jquery程序設(shè)計(jì)有所幫助。
- jQuery實(shí)現(xiàn)選項(xiàng)卡切換效果簡(jiǎn)單演示
- jQuery實(shí)現(xiàn)的Tab滑動(dòng)選項(xiàng)卡及圖片切換(多種效果)小結(jié)
- jQuery實(shí)現(xiàn)TAB選項(xiàng)卡切換特效簡(jiǎn)單演示
- jQuery簡(jiǎn)單實(shí)現(xiàn)tab選項(xiàng)卡切換效果
- 基于jquery實(shí)現(xiàn)最簡(jiǎn)單的選項(xiàng)卡切換效果
- jquery自動(dòng)切換tabs選項(xiàng)卡的具體實(shí)現(xiàn)
- jQuery實(shí)現(xiàn)滾動(dòng)切換的tab選項(xiàng)卡效果代碼
- jquery編寫Tab選項(xiàng)卡滾動(dòng)導(dǎo)航切換特效
- jquery實(shí)現(xiàn)選項(xiàng)卡切換代碼實(shí)例
- jQuery實(shí)現(xiàn)選項(xiàng)卡切換圖片
相關(guān)文章
jQuery回調(diào)函數(shù)的定義及用法實(shí)例
這篇文章主要介紹了jQuery回調(diào)函數(shù)的定義及用法,以實(shí)例形式詳細(xì)分析了回調(diào)函數(shù)的原理與實(shí)現(xiàn)技巧,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12基于jquery實(shí)現(xiàn)五星好評(píng)
這篇文章主要為大家詳細(xì)介紹了基于jquery實(shí)現(xiàn)五星好評(píng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11前端開發(fā)部分總結(jié)[兼容性、DOM操作、跨域等](持續(xù)更新)
在公司做這個(gè)項(xiàng)目已經(jīng)6個(gè)多月了,總結(jié)一些問題,也算是拋磚引玉吧,希望更多的朋友一起分享一些技巧。2010-03-03jquery操作checkbox實(shí)現(xiàn)全選和取消全選
這篇文章主要介紹了jquery操作checkbox實(shí)現(xiàn)全選和取消全選,需要的朋友可以參考下2014-05-05jQuery復(fù)合事件結(jié)合toggle()方法的用法示例
這篇文章主要介紹了jQuery復(fù)合事件結(jié)合toggle()方法的用法,實(shí)例分析了toggle()方法的功能、定義以及與復(fù)合事件結(jié)合使用的操作技巧,需要的朋友可以參考下2017-06-06jquery讀取xml文件實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)的方法
這篇文章主要介紹了jquery讀取xml文件實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)的方法,涉及jQuery操作XML文件及Ajax動(dòng)態(tài)加載的技巧,需要的朋友可以參考下2015-05-05Jquery的Tabs內(nèi)容輪換效果實(shí)現(xiàn)代碼,幾行搞定
本篇文章主要是對(duì)Jquery的Tabs內(nèi)容輪換效果的實(shí)現(xiàn)代碼進(jìn)行了介紹。幾行代碼輕松搞定2014-02-02