jquery插件制作 手風(fēng)琴Panel效果實(shí)現(xiàn)
更新時間:2012年08月17日 15:40:58 作者:
我們今天要做的是手風(fēng)琴panel,jquery.ui里面有個叫做accordtion的插件,我們要實(shí)現(xiàn)的效果和他一樣
首先我們還是創(chuàng)建一個html文件,里面包含如下的html結(jié)構(gòu)。
<div id="pane-container">
<div class="pane">
<h1>first pane</h1>
<p>this script should allow only one pane to be visible at a time.</p>
</div>
<div class="pane">
<h1>second pane</h1>
<p>this script should allow only one pane to be visible at a time.</p>
</div>
<div class="pane">
<h1>third pane</h1>
<p>this script should allow only one pane to be visible at a time.</p>
</div>
</div>
然后為頁面定義如下css:
<style type="text/css">
#pane-container
{
margin: 0;
padding: 0;
width: 200px;
}
.pane h1
{
padding: 5px;
cursor: pointer;
position: relative;
background-color: #4c4c4c;
color: #fff;
font-weight: normal;
font-size: 20px;
margin: 0px;
}
.pane p
{
padding: 10px;
margin: 0;
background-color: #e4e4e4;
}
</style>
下面我們來介紹jquery.accordtion.js插件的實(shí)現(xiàn)。首先我們需要考慮的是如何隱藏pane里面的內(nèi)容部分,也就是p標(biāo)簽。h1作為標(biāo)題是不需要隱藏的。
(function ($) {
$.fn.accordtion = function () {
return this.each(function () {
$(this).find('p').hide();
});
}
})(jQuery);
頁面調(diào)用的代碼:
$(document).ready(function () {
$('#pane-container').accordtion();
});
插件代碼很簡單,就是找到class為pane的div下面的p,對其隱藏。接下來我們要實(shí)現(xiàn)的是,當(dāng)用戶點(diǎn)到h1的時候,對應(yīng)的p標(biāo)簽的內(nèi)容顯示出來,同時其他h1對應(yīng)的p標(biāo)簽的內(nèi)容隱藏。代碼如下:
//對h1標(biāo)簽設(shè)置click事件
self.delegate('h1', 'click', function () {
//為對應(yīng)的p標(biāo)簽設(shè)置slideToggle效果
$(this).next('p').slideToggle(600)
//獲取其他pane對象,找到他們下面的p標(biāo)簽,收起
.parent().siblings().children('p').slideUp(600);
});
現(xiàn)在我們的插件已經(jīng)很有型了,最后要做的就是添加用戶自定義屬性options,這次我們只添加一個‘默認(rèn)顯示第幾個pane'的屬性。
//設(shè)置第幾個元素顯示
self.children(':eq(' + options.visibleByDefault + ')')//找到和options.visibleByDefault一致的pane對象
.children('p')
.show();
完整的代碼大家還是下demo自己看吧。jQuery.plugin.accordtion
謝謝大家的支持,希望本篇文章對你有幫助。如果對代碼哪里有不清楚的地方,可以聯(lián)系我。
復(fù)制代碼 代碼如下:
<div id="pane-container">
<div class="pane">
<h1>first pane</h1>
<p>this script should allow only one pane to be visible at a time.</p>
</div>
<div class="pane">
<h1>second pane</h1>
<p>this script should allow only one pane to be visible at a time.</p>
</div>
<div class="pane">
<h1>third pane</h1>
<p>this script should allow only one pane to be visible at a time.</p>
</div>
</div>
然后為頁面定義如下css:
復(fù)制代碼 代碼如下:
<style type="text/css">
#pane-container
{
margin: 0;
padding: 0;
width: 200px;
}
.pane h1
{
padding: 5px;
cursor: pointer;
position: relative;
background-color: #4c4c4c;
color: #fff;
font-weight: normal;
font-size: 20px;
margin: 0px;
}
.pane p
{
padding: 10px;
margin: 0;
background-color: #e4e4e4;
}
</style>
下面我們來介紹jquery.accordtion.js插件的實(shí)現(xiàn)。首先我們需要考慮的是如何隱藏pane里面的內(nèi)容部分,也就是p標(biāo)簽。h1作為標(biāo)題是不需要隱藏的。
復(fù)制代碼 代碼如下:
(function ($) {
$.fn.accordtion = function () {
return this.each(function () {
$(this).find('p').hide();
});
}
})(jQuery);
頁面調(diào)用的代碼:
復(fù)制代碼 代碼如下:
$(document).ready(function () {
$('#pane-container').accordtion();
});
插件代碼很簡單,就是找到class為pane的div下面的p,對其隱藏。接下來我們要實(shí)現(xiàn)的是,當(dāng)用戶點(diǎn)到h1的時候,對應(yīng)的p標(biāo)簽的內(nèi)容顯示出來,同時其他h1對應(yīng)的p標(biāo)簽的內(nèi)容隱藏。代碼如下:
復(fù)制代碼 代碼如下:
//對h1標(biāo)簽設(shè)置click事件
self.delegate('h1', 'click', function () {
//為對應(yīng)的p標(biāo)簽設(shè)置slideToggle效果
$(this).next('p').slideToggle(600)
//獲取其他pane對象,找到他們下面的p標(biāo)簽,收起
.parent().siblings().children('p').slideUp(600);
});
現(xiàn)在我們的插件已經(jīng)很有型了,最后要做的就是添加用戶自定義屬性options,這次我們只添加一個‘默認(rèn)顯示第幾個pane'的屬性。
復(fù)制代碼 代碼如下:
//設(shè)置第幾個元素顯示
self.children(':eq(' + options.visibleByDefault + ')')//找到和options.visibleByDefault一致的pane對象
.children('p')
.show();
完整的代碼大家還是下demo自己看吧。jQuery.plugin.accordtion
謝謝大家的支持,希望本篇文章對你有幫助。如果對代碼哪里有不清楚的地方,可以聯(lián)系我。
您可能感興趣的文章:
- jQuery Easyui使用(一)之可折疊面板的布局手風(fēng)琴菜單
- jQuery實(shí)現(xiàn)的簡單手風(fēng)琴效果示例
- jQuery制作效果超棒的手風(fēng)琴折疊菜單
- 基于Jquery代碼實(shí)現(xiàn)手風(fēng)琴菜單
- 基于jquery的slideDown和slideUp做手風(fēng)琴
- jquery實(shí)現(xiàn)手風(fēng)琴效果實(shí)例代碼
- jquery手風(fēng)琴特效插件
- Jquery組件easyUi實(shí)現(xiàn)手風(fēng)琴(折疊面板)示例
- jquery超簡單實(shí)現(xiàn)手風(fēng)琴效果的方法
- jquery實(shí)現(xiàn)手風(fēng)琴展開效果
相關(guān)文章
jquery動畫4.升級版遮罩效果的圖片走廊--帶自動運(yùn)行效果
我將上一章中了插件做了個小小的升級,實(shí)現(xiàn)了自動運(yùn)行效果,完整代碼大家見demo2012-08-08
jQuery焦點(diǎn)圖切換簡易插件制作過程全紀(jì)錄
本文主要講訴了本人制作一個jquery焦點(diǎn)圖切換的簡易插件的過程,十分的詳細(xì),希望對大家能有所幫助2014-08-08
jQuery實(shí)現(xiàn)轉(zhuǎn)動隨機(jī)數(shù)抽獎效果的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)轉(zhuǎn)動隨機(jī)數(shù)抽獎效果的方法,涉及jQuery操作隨機(jī)數(shù)及頁面元素的相關(guān)技巧,需要的朋友可以參考下2015-05-05
jQuery插件實(shí)現(xiàn)彈性運(yùn)動完整示例
這篇文章主要介紹了jQuery插件實(shí)現(xiàn)彈性運(yùn)動,結(jié)合完整實(shí)例形式分析了jQuery插件擴(kuò)展結(jié)合定時器實(shí)現(xiàn)頁面元素抖動的相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
jquery實(shí)現(xiàn)垂直和水平菜單導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)垂直和水平菜單導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08
jQuery插件artDialog.js使用與關(guān)閉方法示例
這篇文章主要介紹了jQuery插件artDialog.js使用與關(guān)閉方法,結(jié)合具體實(shí)例形式分析了jQuery彈出窗口插件artDialog.js的簡單使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-10-10
jQuery實(shí)現(xiàn)QQ空間漢字轉(zhuǎn)拼音功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)QQ空間漢字轉(zhuǎn)拼音功能,結(jié)合具體實(shí)例形式分析了jQuery實(shí)現(xiàn)拼音與中文漢字的轉(zhuǎn)換操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-07-07

