基于jQuery实现的Accordion手风琴自定义插件

目前网上有很多各种各样的手风琴插件,但是没有一个完整实现了的侧菜单,今天写了一个可以无限子节点的手风琴侧菜单,有需要的可以参考一下,有什么好的想法可以留言。(没有经过彻底测试,不过问题应该不大)

下面老规矩,直接贴代码:

  1 (function ($) {
  2     ‘use strict‘;
  3     var defaults = {
  4         url: null,
  5         param: {},
  6         data: {},
  7         fill: true,
  8         level_space: 15,
  9         onitemclick: null,
 10         style: {
 11             header: "accordion-header",
 12             header_title: "accordion-header-title",
 13             content: "accordion-content",
 14             selected: "selected",
 15             icon_base: "fa",
 16             icon_collapse: "fa-angle-up",
 17             icon_expand: "fa-angle-down"
 18         }
 19     }
 20     var methods = {
 21         init: function (options) {
 22             return this.each(function () {
 23                 var $this = $(this);
 24                 if (!$this.hasClass("accordion")) {
 25                     $this.addClass("accordion");
 26                 }
 27                 var settings = $this.data(‘tw.accordion‘);
 28                 if (typeof (settings) == ‘undefined‘) {
 29                     settings = $.extend({}, defaults, options);
 30                     $this.data(‘tw.accordion‘, settings);
 31                 } else {
 32                     settings = $.extend({}, settings, options);
 33                     $this.data(‘tw.accordion‘, settings);
 34                 }
 35                 if (settings.url) {
 36                     $.ajax({
 37                         type: "post",
 38                         async: false,
 39                         url: settings.url,
 40                         data: settings.param,
 41                         success: function (data) {
 42                             settings.data = data;
 43                         }
 44                     });
 45                 }
 46                 if (settings.fill) {
 47                     $this.height("100%");
 48                 }
 49                 if (settings.data.length > 0) {
 50                     $this.data("count", settings.data.length);
 51                     $.each(settings.data, function () {
 52                         this.level = 1;
 53                         var item = $this.accordion("add", this);
 54                         $this.append(item);
 55                     });
 56                     if ($this.find("." + settings.style.selected).length == 0) {
 57                         var data = $this.find(">li:first-child").data("data");
 58                         $this.accordion("select", data);
 59                     }
 60                 }
 61             });
 62         },
 63         add: function (data) {
 64             var $this = $(this);
 65             var settings = $this.data("tw.accordion");
 66             var item = $("<li class=‘" + settings.style.header + "‘></li>");
 67             item.data("data", data);
 68             var header = $("<div class=‘" + settings.style.header_title + "‘ data-accordion=‘" + data.id + "‘>" +
 69                 "<i class=‘" + settings.style.icon_base + "" + data.icon + "‘></i>" +
 70                 "<span>" + data.name + "</span></div>");
 71             header.css("padding-left", settings.level_space * data.level);
 72             item.append(header);
 73             if (data.childrens) {
 74                 var toggle = $("<i class=‘" + settings.style.icon_base + "" + settings.style.icon_collapse + "‘></i>");
 75                 toggle.css({ "font-size": "1.4em", "position": "absolute", "top": "7px", "right": "7px" });
 76                 header.append(toggle);
 77                 var content = $("<ul class=‘" + settings.style.content + "‘></ul>");
 78                 content.data("count", data.childrens.length);
 79                 $.each(data.childrens, function () {
 80                     this.level = data.level + 1;
 81                     var child = $this.accordion("add", this);
 82                     content.append(child);
 83                 });
 84                 item.append(content);
 85             }
 86             header.click(function () {
 87                 $this.accordion("select", data);
 88             });
 89             if (data.selected) {
 90                 $this.accordion("select", data);
 91             }
 92             return item;
 93         },
 94         select: function (data) {
 95             var $this = $(this);
 96             var settings = $this.data("tw.accordion");
 97             var header = $this.find("[data-accordion=‘" + data.id + "‘]");
 98             var item = header.parent();
 99             if (!header.hasClass(settings.style.selected) && !item.hasClass(settings.style.selected)) {
100                 var sibling = item.siblings();
101                 sibling.removeClass(settings.style.selected).children("." + settings.style.selected).removeClass(settings.style.selected);
102                 sibling.children("." + settings.style.icon_expand).removeClass(settings.style.icon_expand).addClass(settings.style.icon_collapse);
103                 if (data.childrens) {
104                     item.addClass(settings.style.selected);
105                     header.find("." + settings.style.icon_collapse).removeClass(settings.style.icon_collapse).addClass(settings.style.icon_expand);
106                     if (settings.fill) {
107                         var count = item.parent().data("count") - 1;
108                         item.css("height", "calc(100% - " + (item.height() * count) + "px)");
109                     }
110                 } else {
111                     header.addClass(settings.style.selected);
112                 }
113             }
114             if (settings.onitemclick) {
115                 settings.onitemclick(data);
116             }
117         },
118         update: function (url, param) {
119             var $this = $(this);
120             var settings = $this.data("tw.accordion");
121             if (typeof url == "object") {
122                 settings.param = url;
123             } else {
124                 settings.url = url;
125                 settings.param = param;
126             }
127             $this.accordion("init", settings);
128         },
129         destroy: function (options) {
130             return $(this).each(function () {
131                 var $this = $(this);
132                 $this.removeData(‘accordion‘);
133             });
134         }
135     }
136     $.fn.accordion = function () {
137         var method = arguments[0];
138         var args = arguments;
139         if (typeof (method) == ‘object‘ || !method) {
140             method = methods.init;
141         } else if (methods[method]) {
142             method = methods[method];
143             args = $.makeArray(arguments).slice(1);
144         } else {
145             $.error(‘Method ‘ + method + ‘ does not exist on tw.accordion‘);
146             return this;
147         }
148         return method.apply(this, args);
149     }
150 })(jQuery);
  1 .accordion {
  2     margin:0;
  3     padding:0;
  4     font-size:14px;
  5 }
  6     .accordion > .accordion-header {
  7         list-style: none;
  8         margin: 0;
  9         padding: 0;
 10         border-bottom: 1px solid #ddd;
 11     }
 12         .accordion > .accordion-header.selected > .accordion-header-title {
 13             color: #0094ff;
 14         }
 15         .accordion > .accordion-header > .accordion-header-title {
 16             position: relative;
 17             width: 100%;
 18             height: 35px;
 19             line-height: 35px;
 20             background: #eee;
 21             border-bottom: 1px solid #ccc;
 22             cursor: pointer;
 23         }
 24             .accordion > .accordion-header > .accordion-header-title > i:first-child {
 25                 font-size: 1.3em;
 26             }
 27             .accordion > .accordion-header > .accordion-header-title > span {
 28                 position: relative;
 29                 top: -1px;
 30                 left: 5px;
 31             }
 32         .accordion > .accordion-header > .accordion-content {
 33             display: none;
 34             width: 100%;
 35             height: calc(100% - 35px);
 36             margin: 0;
 37             padding: 0;
 38         }
 39         .accordion > .accordion-header.selected > .accordion-content {
 40             display: block;
 41         }
 42 .accordion-content > .accordion-header {
 43         list-style: none;
 44         margin: 0;
 45         padding: 0;
 46 }
 47     .accordion-content > .accordion-header.selected {
 48         color: #0094ff;
 49     }
 50     .accordion-content > .accordion-header > .accordion-header-title {
 51         position: relative;
 52         width: 100%;
 53         height: 32px;
 54         line-height: 32px;
 55         cursor: pointer;
 56         border-bottom: 1px solid #ccc;
 57     }
 58         .accordion-content > .accordion-header > .accordion-header-title:hover {
 59             background:#eee;
 60         }
 61         .accordion-content > .accordion-header > .accordion-header-title.selected {
 62             color: #fff;
 63             background: #0094ff;
 64             border-left: 3px solid #ff6a00;
 65             border-bottom: 0px;
 66         }
 67             .accordion-content > .accordion-header > .accordion-header-title > i:first-child {
 68                 font-size: 1.2em;
 69             }
 70             .accordion-content > .accordion-header > .accordion-header-title > span {
 71                 position: relative;
 72                 top: -1px;
 73                 left: 5px;
 74             }
 75             .accordion-content > .accordion-header > .accordion-header-title.selected > i:first-child {
 76                 position:relative;
 77                 left:-3px;
 78             }
 79             .accordion-content > .accordion-header > .accordion-header-title.selected > span {
 80                 position: relative;
 81                 top: -1px;
 82                 left: 2px;
 83             }
 84         .accordion-content > .accordion-header > .accordion-content {
 85             display: none;
 86             width: 100%;
 87             height: calc(100% - 32px);
 88             margin: 0;
 89             padding: 0;
 90         }
 91         .accordion-content > .accordion-header.selected > .accordion-content {
 92             display: block;
 93         }
时间: 2024-10-28 11:23:15

基于jQuery实现的Accordion手风琴自定义插件的相关文章

基于jQuery实现的Tabs选项卡自定义插件

控件这东西网上找的多少都有些功能不如意的,只能自己动手来写了. Tabs插件很多人都需要用到,网上也有很多的例子,但是不是风格不搭调就是用着不习惯(自己写的不一定比别人的好但用着顺手) 下面直接贴代码,不喜勿喷: 1 (function ($) { 2 'use strict'; 3 var defaults = { 4 type: "iframe", 5 onchanged: null, 6 style: { 7 header_panel: "tab-headers&quo

基于jQuery多层次的手风琴代码

基于jQuery多层次的手风琴代码是一款经过美化的多级多层次手风琴特效代码.效果图如下: 在线预览   源码下载 实现的代码. html代码: <aside class="accordion"> <h1>News</h1> <div class="opened-for-codepen"> <h2>News Item #1</h2> <div class="opened-for-c

转载:基于jquery的bootstrap在线文本编辑器插件Summernote

基于jquery的bootstrap在线文本编辑器插件Summernote 转载:jQ酷 » 基于jquery的bootstrap在线文本编辑器插件Summernote Summernote是一个基于jquery的bootstrap超级简单WYSIWYG在线编辑器.Summernote非常的轻量级,大小只有30KB,支持Safari,Chrome,Firefox.Opera.Internet Explorer 9 +(IE8支持即将到来). 特点: 世界上最好的WYSIWYG在线编辑器 极易安装

基于jQuery select下拉框美化插件

今天给大家分享一款基于jQuery select下拉框美化插件,这款插件适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗.  <table>         <tr>             <td>                 <h2>                     演示1</h2>                 <select name="drop1&qu

jQuery补充,基于jQuery的bxslider轮播器插件

基于jQuery的bxslider轮播器插件 html <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=n

基于jQuery全屏焦点图切换插件responsiveslides

基于jQuery全屏焦点图切换插件responsiveslides是一款带左右箭头,索引按钮的自动轮播切换特效下载.效果图如下: 在线预览   源码下载 实现的代码. html代码: <script type="text/javascript"> $(function () { // Slideshow $("#slider").responsiveSlides({ auto: true, pager: false, nav: true, speed:

基于jQuery点击图像居中放大插件Zoom

分享一款基于jQuery点击图像居中放大插件Zoom是一款放大的时候会从原图像的位置以动画方式放大到画面中间,支持点击图像或者按ESC键来关闭效果.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class="page-body"> <div style="width: 80px; height: 80px; margin-right: 10px; padding: 10px; float: left; background:

基于jquery的bootstrap在线文本编辑器插件Summernote 简单强大

Summernote是一个基于jquery的bootstrap超级简单WYSIWYG在线编辑器.Summernote非常的轻量级,大小只有30KB,支持Safari,Chrome,Firefox.Opera.Internet Explorer 9 +(IE8支持即将到来). 特点: 世界上最好的WYSIWYG在线编辑器 极易安装 开源 自定义初化选项 支持快捷键 适用于各种后端程序言语 使用方法 使用html5文档 1 2 3 4 <!DOCTYPE html> <html> ..

10款基于jQuery的图片滑块焦点图插件

1.Material UI – Material Design CSS 框架 Material Design 是谷歌推出的全新的设计理念,采用大胆的色彩.流畅的动画播放,以及卡片式的简洁设计.Material Design 风格的设计拥有干净的排版和简单的布局,容易理解,内容才是焦点.Material UI 是一个 CSS 框架和一组实现谷歌 Material Design 设计规范的 React 组件.可以作为 NPM 安装包,使用 browserify 和 reactify 的依赖管理和 J