[js插件開發教程]定製一個手風琴插件(accordion)

来源:http://www.cnblogs.com/ghostwu/archive/2017/10/24/7722483.html
-Advertisement-
Play Games

本文帶來一個垂直方向的手風琴插件開發,可以定製的功能如下: 調用方法: 效果預覽: 效果預覽: 完整的手風琴插件代碼: html部分: 1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <!--作者:ghostwu(吳華)--> 5 <meta char ...


本文帶來一個垂直方向的手風琴插件開發,可以定製的功能如下:

contentClass : 'panel',    //面板樣式
navClass : 'nav', //導航樣式
activeClass : 'active', //導航激活樣式
triggerElements : '*', //觸發元素
activeIndex : 0, //預設選中的元素
evType : 'click', //預設觸發的事件
animate : true, //是否支持動畫漸變
multiple : false //是否支持多個面板同時展開

調用方法:

$(".accordion").accordion( { animate : true, multiple : true } );   //支持多個面板同時展開, 支持動畫效果

效果預覽:

 

$(".accordion").accordion( { animate : false, multiple : true } );
這種效果就是跟選項卡一樣

$(".accordion").accordion( { 'animate' : true } );
不支持多個面板,永遠只有一個面板被打開

效果預覽:

完整的手風琴插件代碼:

html部分:

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <!--作者:ghostwu(吳華)-->
 5     <meta charset="UTF-8">
 6     <title></title>
 7     <link rel="stylesheet" href="./css/accordion.css"/>
 8     <script src="./js/accordion.js"></script>
 9     <script>
10         window.onload = function(){
11             $(".accordion").accordion( { 'animate' : true } );
12         }
13     </script>
14 </head>
15 <body>
16     <div class="main">
17         <div class="accordion">
18             <div class="panel">面板1</div>
19             <div class="nav">導航1</div>
20             <div class="panel">面板2</div>
21             <div class="nav">導航2</div>
22             <div class="panel">面板3</div>
23             <div class="nav">導航3</div>
24             <div class="panel">面板4</div>
25             <div class="nav">導航4</div>
26             <div class="panel">面板5</div>
27             <div class="nav">導航5</div>
28         </div>
29     </div>
30 </body>
31 </html>
View Code

css樣式部分:

 1 * {
 2     margin: 0;
 3     padding: 0;
 4 }
 5 body {
 6     font-size: 14px;
 7     font-family: Tahoma, Verdana,"Microsoft Yahei";
 8 }
 9 a{
10     text-decoration: none;
11     color:#000;
12 }
13 ul,li{
14     list-style-type: none;
15 }
16 img {
17     border:none;
18 }
19 .main {
20     width:960px;
21     margin:20px auto;
22 }
23 
24 .accordion{
25     width:400px;
26     margin: 0 auto 10px;
27 }
28 
29 .accordion .nav {
30     border-bottom: 1px dashed #0064cd;
31 }
32 .accordion .panel{
33     height:100px;
34     background:#ccc;
35 }
36 .accordion .nav{
37     height:30px;
38     background:#999;
39 }
40 .accordion .active{
41     background:#ff0;
42 }
View Code

js插件部分:

  1 /**
  2  * Created by ghostwu(吳華).
  3  */
  4 (function(){
  5     var G = function( selectors, context ){
  6         return new G.fn.init( selectors, context );
  7     }
  8     G.fn = G.prototype = {
  9         length : 0,
 10         constructor : G,
 11         size : function(){
 12             return this.length;
 13         },
 14         init : function( selector, context ){
 15             this.length = 0;
 16             context = context || document;
 17             if ( selector.indexOf( '#' ) == 0 ){
 18                 this[0] = document.getElementById( selector.substring( 1 ) );
 19                 this.length = 1;
 20             }else {
 21                 var aNode = context.querySelectorAll( selector );
 22                 for( var i = 0, len = aNode.length; i < len; i++ ) {
 23                     this[i] = aNode[i];
 24                 }
 25                 this.length = len;
 26             }
 27             this.selector = selector;
 28             this.context = context;
 29             return this;
 30         }
 31     }
 32 
 33     G.fn.init.prototype = G.fn;
 34     G.extend = G.fn.extend = function () {
 35         var i = 1,
 36             len = arguments.length,
 37             dst = arguments[0],
 38             j;
 39         if (dst.length === undefined) {
 40             dst.length = 0;
 41         }
 42         if (i == len) {
 43             dst = this;
 44             i--;
 45         }
 46         for (; i < len; i++) {
 47             for (j in arguments[i]) {
 48                 dst[j] = arguments[i][j];
 49                 dst.length++;
 50             }
 51         }
 52         return dst;
 53     };
 54 
 55     function css(obj, attr, value) {
 56         if (arguments.length == 3) {
 57             obj.style[attr] = value;
 58         } else {
 59             if (obj.currentStyle) {
 60                 return obj.currentStyle[attr];
 61             } else {
 62                 return getComputedStyle(obj, false)[attr];
 63             }
 64         }
 65     }
 66 
 67     function animate(obj, attr, fn) {
 68         clearInterval(obj.timer);
 69         var cur = 0;
 70         var target = 0;
 71         var speed = 0;
 72         //var start = new Date().getTime();//起始時間
 73         obj.timer = setInterval(function () {
 74             var bFlag = true;
 75             for (var key in attr) {
 76                 if (key == 'opacity') {
 77                     cur = css(obj, 'opacity') * 100;
 78                 } else {
 79                     cur = parseInt(css(obj, key));
 80                 }
 81                 target = attr[key];
 82                 speed = ( target - cur ) / 8;
 83                 speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
 84                 console.log( cur, target );
 85                 if (cur != target) {
 86                     bFlag = false;
 87                     if (key == 'opacity') {
 88                         obj.style.opacity = ( cur + speed ) / 100;
 89                         obj.style.filter = "alpha(opacity:" + ( cur + speed ) + ")";
 90                     } else {
 91                         obj.style[key] = cur + speed + "px";
 92                     }
 93                 }
 94             }
 95             if (bFlag) {
 96                 //var end = new Date().getTime();//結束時間
 97                 //console.log( '總計:',  ( end - start ) );
 98                 clearInterval(obj.timer);
 99                 fn && fn.call(obj);
100             }
101         }, 30 );
102     }
103 
104     G.fn.accordion = function( options ){
105         options = options || {};
106         var defaults = {
107             contentClass : 'panel',
108             navClass : 'nav',
109             activeClass : 'active',
110             triggerElements : '*',
111             activeIndex : 0,
112             evType : 'click',
113             animate : true,
114             multiple : false
115         };
116 
117         var opt = G.extend( {}, defaults, options );
118 
119         var aNavEle = this[0].querySelectorAll( "." + opt.navClass ),
120             aPanel = this[0].querySelectorAll( "." + opt.contentClass );
121 
122         var _api = {};
123 
124         var panelHeight = parseInt( css( aPanel[opt.activeIndex], 'height' ) );
125         _api.setIndex = function( curIndex, effect ){
126             if ( opt.multiple ) {
127                 if ( effect ) {
128                     if ( parseInt( css( aPanel[curIndex], 'height' ) ) == 0 ) {
129                         aPanel[curIndex].style.display = 'block';
130                         animate( aPanel[curIndex], { 'height' : panelHeight } );
131                     }else {
132                         animate( aPanel[curIndex], { 'height' : 0 }, function(){
133                             this.style.display = 'none';
134                         } );
135                     }
136                 }else {
137                     if ( css( aPanel[curIndex], 'display' ) == 'block' ){
138                         aPanel[curIndex].style.display = 'none';
139                     }else {
140                         aPanel[curIndex].style.display = 'block';
141                     }
142                     if ( aNavEle[curIndex].classList.contains( 'active' ) ) {
143                         aNavEle[curIndex].classList.remove( 'active' );
144                     }else {
145                         aNavEle[curIndex].classList.add( 'active' );
146                     }
147                 }
148             }else {
149                 if ( effect ) {
150                     for ( var i = 0; i < aPanel.length; i++ ) {
151                         if( i != curIndex ) {
152                             if ( aPanel[i].style.display == 'block' ){
153                                 animate( aPanel[i], { 'height' : 0 }, function(){
154                                     this.style.display = 'none';
155                                 } );
156                             }else{
157                                 aPanel[i].style.display = 'none';
158                             }
159                         }else {
160                             aPanel[curIndex].style.display = 'block';
161                             animate( aPanel[curIndex], { 'height' : panelHeight } );
162                         }
163                     }
164                 }else {
165                     for ( var i = 0 ; i < aPanel.length; i++ ){
166                         aPanel[i].style.display = 'none';
167                         aNavEle[i].classList.remove( 'active' );
168                     }
169                     aPanel[curIndex].style.display = 'block';
170                     aNavEle[curIndex].classList.add( 'active' );
171                 }
172             }
173         };
174 
175         _api.setIndex( opt.activeIndex, opt.animate );
176 
177         for( var i = 0 ; i < aNavEle.length; i++ ) {
178             aNavEle[i].index = i;
179             aNavEle[i].addEventListener( opt.evType, function(){
180                 _api.setIndex( this.index, opt.animate );
181             }, false );
182         }
183         return this;
184     }
185 
186     var $ = function( selectors, context ){
187         return G( selectors, context );
188     }
189     window.$ = $;
190 })();
View Code

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • jsp為用戶提供了9個內置對象,該內置對象由容器進行實例化,用戶直接使用即可; 1 四種屬性範圍 所謂的屬性保存範圍:指一個設置的對象可以在幾個頁面中保存可以繼續使用; 屬性操作方法: public void setAttribute(String name, Object obj) 設置屬性的名稱 ...
  • 1、算數運算 2、比較減運算 3、賦值運算 4、邏輯運算 5、成員運算 ...
  • 十一假期後就有點懶散,好長時間都沒想起來寫東西了。另外最近在打LOL的S賽。接觸LOL時間不長,雖然平時玩的比較少,水平也相當菜,但是像這種大型的賽事有時間還是不會錯過的。主要能夠感受到選手們對競技的激情,對瞬息萬變的戰局的應變,非常精彩。KeKe~~。 這一篇主要對UVW的源碼討論來收個尾,就介紹 ...
  • ORM是O和R的映射。O代錶面向對象,R代表關係型資料庫。二者有相似之處同時也各有特色。就是因為這種即是又非的情況,才需要做映射的。 理想情況是,根據關係型資料庫(含業務需求)的特點來設計資料庫。同時根據面向對象(含業務需求)的特點來設計模型(實體類)。然後再去考慮如何做映射。但是理想很骨jian感 ...
  • 我所理解的DUBBO 相對於傳統web開發框架,dubbo更加適合於並行系統開發,分散式,模塊化。將server和client都註冊到zookeeper註冊中心上,然後由最外層客戶端發起請求到相應client上,client再調用server。所謂模塊化,舉例說明,將一個電商系統分隔成用戶,商品,進 ...
  • A代碼編輯器,線上模版編輯,仿開發工具編輯器,pdf線上預覽,文件轉換編碼B 集成代碼生成器 [正反雙向](單表、主表、明細表、樹形表,快速開發利器)+快速表單構建器 freemaker模版技術 ,0個代碼不用寫,生成完整的一個模塊,帶頁面、建表sql腳本,處理類,service等完整模塊C 集成阿 ...
  • 一、vue腳手架跨域問題 1.跨域問題 文件config/index.js 原始代碼: proxyTable修改如下: "參考資料" 2.遠程訪問問題 目的:寫好的項目希望在其他電腦上或手機上查看。 方法: 打開文件 config/index.js,在dev對象中添加一個屬性host,值為本機的ip ...
  • 最近火的不能再火的日誌框架就是ELK,其中E(Elasticsearch)表示日誌存儲,L(Logstash)表示日誌收集,K(kibana)表示日誌的UI界面,用來查詢和分析,而其中的L可以使用Fluentd來代替,並且以上架構都可以通過docker來進行快速的部署。 它們的工作流程 fluent ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...