目前網路上有很多各種各樣的手風琴插件,但是沒有一個完整實現了的側菜單,今天寫了一個可以無限子節點的手風琴側菜單,有需要的可以參考一下,有什麼好的想法可以留言。 (沒有經過徹底測試,不過問題應該不大)
下面老規矩,直接貼代碼:
(function ($) { 'use strict'; var defaults = { url: null, param: {}, data: {}, fill: true, level_space: 15, onitemclick: null, style: { header: "accordion-header", header_title: "accordion-header-title", content: "accordion-content", selected: "selected", icon_base: "fa", icon_collapse: "fa-angle-up", icon_expand: "fa-angle-down" } } var methods = { init: function (options) { return this.each(function () { var $this = $(this); if (!$this.hasClass("accordion")) { $this.addClass("accordion"); } var settings = $this.data('tw.accordion'); if (typeof (settings) == 'undefined') { settings = $.extend({}, defaults, options); $this.data('tw.accordion', settings); } else { settings = $.extend({}, settings, options); $this.data('tw.accordion', settings); } if (settings.url) { $.ajax({ type: "post", async: false, url: settings.url, data: settings.param, success: function (data) { settings.data = data; } }); } if (settings.fill) { $this.height("100%"); } if (settings.data.length > 0) { $this.data("count", settings.data.length); $.each(settings.data, function () { this.level = 1; var item = $this.accordion("add", this); $this.append(item); }); if ($this.find("." + settings.style.selected).length == 0) { var data = $this.find(">li:first-child").data("data"); $this.accordion("select", data); } } }); }, add: function (data) { var $this = $(this); var settings = $this.data("tw.accordion"); var item = $("<li class='" + settings.style.header + "'></li>"); item.data("data", data); var header = $("<div class='" + settings.style.header_title + "' data-accordion='" + data.id + "'>" + "<i class='" + settings.style.icon_base + "" + data.icon + "'></i>" + "<span>" + data.name + "</span></div>"); header.css("padding-left", settings.level_space * data.level); item.append(header); if (data.childrens) { var toggle = $("<i class='" + settings.style.icon_base + "" + settings.style.icon_collapse + "'></i>"); toggle.css({ "font-size": "1.4em", "position": "absolute", "top": "7px", "right": "7px" }); header.append(toggle); var content = $("<ul class='" + settings.style.content + "'></ul>"); content.data("count", data.childrens.length); $.each(data.childrens, function () { this.level = data.level + 1; var child = $this.accordion("add", this); content.append(child); }); item.append(content); } header.click(function () { $this.accordion("select", data); }); if (data.selected) { $this.accordion("select", data); } return item; }, select: function (data) { var $this = $(this); var settings = $this.data("tw.accordion"); var header = $this.find("[data-accordion='" + data.id + "']"); var item = header.parent(); if (!header.hasClass(settings.style.selected) && !item.hasClass(settings.style.selected)) { var sibling = item.siblings(); sibling.removeClass(settings.style.selected).children("." + settings.style.selected).removeClass(settings.style.selected); sibling.children("." + settings.style.icon_expand).removeClass(settings.style.icon_expand).addClass(settings.style.icon_collapse); if (data.childrens) { item.addClass(settings.style.selected); header.find("." + settings.style.icon_collapse).removeClass(settings.style.icon_collapse).addClass(settings.style.icon_expand); if (settings.fill) { var count = item.parent().data("count") - 1; item.css("height", "calc(100% - " + (item.height() * count) + "px)"); } } else { header.addClass(settings.style.selected); } } if (settings.onitemclick) { settings.onitemclick(data); } }, update: function (url, param) { var $this = $(this); var settings = $this.data("tw.accordion"); if (typeof url == "object") { settings.param = url; } else { settings.url = url; settings.param = param; } $this.accordion("init", settings); }, destroy: function (options) { return $(this).each(function () { var $this = $(this); $this.removeData('accordion'); }); } } $.fn.accordion = function () { var method = arguments[0]; var args = arguments; if (typeof (method) == 'object' || !method) { method = methods.init; } else if (methods[method]) { method = methods[method]; args = $.makeArray(arguments).slice(1); } else { $.error('Method ' + method + ' does not exist on tw.accordion'); return this; } return method.apply(this, args); } })(jQuery);
.accordion { margin:0; padding:0; font-size:14px; } .accordion > .accordion-header { list-style: none; margin: 0; padding: 0; border-bottom: 1px solid #ddd; } .accordion > .accordion-header.selected > .accordion-header-title { color: #0094ff; } .accordion > .accordion-header > .accordion-header-title { position: relative; width: 100%; height: 35px; line-height: 35px; background: #eee; border-bottom: 1px solid #ccc; cursor: pointer; } .accordion > .accordion-header > .accordion-header-title > i:first-child { font-size: 1.3em; } .accordion > .accordion-header > .accordion-header-title > span { position: relative; top: -1px; left: 5px; } .accordion > .accordion-header > .accordion-content { display: none; width: 100%; height: calc(100% - 35px); margin: 0; padding: 0; } .accordion > .accordion-header.selected > .accordion-content { display: block; } .accordion-content > .accordion-header { list-style: none; margin: 0; padding: 0; } .accordion-content > .accordion-header.selected { color: #0094ff; } .accordion-content > .accordion-header > .accordion-header-title { position: relative; width: 100%; height: 32px; line-height: 32px; cursor: pointer; border-bottom: 1px solid #ccc; } .accordion-content > .accordion-header > .accordion-header-title:hover { background:#eee; } .accordion-content > .accordion-header > .accordion-header-title.selected { color: #fff; background: #0094ff; border-left: 3px solid #ff6a00; border-bottom: 0px; } .accordion-content > .accordion-header > .accordion-header-title > i:first-child { font-size: 1.2em; } .accordion-content > .accordion-header > .accordion-header-title > span { position: relative; top: -1px; left: 5px; } .accordion-content > .accordion-header > .accordion-header-title.selected > i:first-child { position:relative; left:-3px; } .accordion-content > .accordion-header > .accordion-header-title.selected > span { position: relative; top: -1px; left: 2px; } .accordion-content > .accordion-header > .accordion-content { display: none; width: 100%; height: calc(100% - 32px); margin: 0; padding: 0; } .accordion-content > .accordion-header.selected > .accordion-content { display: block; }
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章
刺客信條陰影:貝殼謎語解決方案
1 個月前ByDDD
Windows 11 KB5054979中的新功能以及如何解決更新問題
3 週前ByDDD
在哪裡可以找到原子中的起重機控制鑰匙卡
1 個月前ByDDD
如何修復KB5055523無法在Windows 11中安裝?
2 週前ByDDD
Inzoi:如何申請學校和大學
3 週前ByDDD

熱工具

SublimeText3漢化版
中文版,非常好用

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。