0

0

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

高洛峰

高洛峰

发布时间:2016-12-06 13:27:49

|

1447人浏览过

|

来源于php中文网

原创

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

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

带数字的图文tab切换特效
带数字的图文tab切换特效

jQuery基于owlCarousel插件实现解决方案图文布局,自定义数字索引控制tab切换效果。这是一款黑色的图文案例展示布局代码。

下载
(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 = $("
  • "); item.data("data", data); var header = $("
    " + "" + "" + data.name + "
    "); header.css("padding-left", settings.level_space * data.level); item.append(header); if (data.childrens) { var toggle = $(""); toggle.css({ "font-size": "1.4em", "position": "absolute", "top": "7px", "right": "7px" }); header.append(toggle); var content = $("
      "); 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工具

      更多
      DeepSeek
      DeepSeek

      幻方量化公司旗下的开源大模型平台

      豆包大模型
      豆包大模型

      字节跳动自主研发的一系列大型语言模型

      通义千问
      通义千问

      阿里巴巴推出的全能AI助手

      腾讯元宝
      腾讯元宝

      腾讯混元平台推出的AI助手

      文心一言
      文心一言

      文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

      讯飞写作
      讯飞写作

      基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

      即梦AI
      即梦AI

      一站式AI创作平台,免费AI图片和视频生成。

      ChatGPT
      ChatGPT

      最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

      相关专题

      更多
      go语言 注释编码
      go语言 注释编码

      本专题整合了go语言注释、注释规范等等内容,阅读专题下面的文章了解更多详细内容。

      2

      2026.01.31

      go语言 math包
      go语言 math包

      本专题整合了go语言math包相关内容,阅读专题下面的文章了解更多详细内容。

      1

      2026.01.31

      go语言输入函数
      go语言输入函数

      本专题整合了go语言输入相关教程内容,阅读专题下面的文章了解更多详细内容。

      1

      2026.01.31

      golang 循环遍历
      golang 循环遍历

      本专题整合了golang循环遍历相关教程,阅读专题下面的文章了解更多详细内容。

      0

      2026.01.31

      Golang人工智能合集
      Golang人工智能合集

      本专题整合了Golang人工智能相关内容,阅读专题下面的文章了解更多详细内容。

      1

      2026.01.31

      2026赚钱平台入口大全
      2026赚钱平台入口大全

      2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。

      76

      2026.01.31

      高干文在线阅读网站大全
      高干文在线阅读网站大全

      汇集热门1v1高干文免费阅读资源,涵盖都市言情、京味大院、军旅高干等经典题材,情节紧凑、人物鲜明。阅读专题下面的文章了解更多详细内容。

      73

      2026.01.31

      无需付费的漫画app大全
      无需付费的漫画app大全

      想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。

      67

      2026.01.31

      漫画免费在线观看地址大全
      漫画免费在线观看地址大全

      想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。

      19

      2026.01.31

      热门下载

      更多
      网站特效
      /
      网站源码
      /
      网站素材
      /
      前端模板

      精品课程

      更多
      相关推荐
      /
      热门推荐
      /
      最新课程
      jQuery 教程
      jQuery 教程

      共42课时 | 5.2万人学习

      HTML+CSS基础与实战
      HTML+CSS基础与实战

      共132课时 | 10万人学习

      tp6+adminlte搭建通用后台
      tp6+adminlte搭建通用后台

      共39课时 | 5.8万人学习

      关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
      php中文网:公益在线php培训,帮助PHP学习者快速成长!
      关注服务号 技术交流群
      PHP中文网订阅号
      每天精选资源文章推送

      Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号