Heim > php教程 > PHP开发 > Implementierung des benutzerdefinierten Accordion-Akkordeon-Plug-Ins basierend auf jQuery

Implementierung des benutzerdefinierten Accordion-Akkordeon-Plug-Ins basierend auf jQuery

高洛峰
Freigeben: 2016-12-06 13:27:49
Original
1342 Leute haben es durchsucht

Es gibt viele verschiedene Akkordeon-Plug-Ins im Internet, aber es gibt kein vollständig implementiertes Seitenmenü mit unbegrenzten untergeordneten Knoten. Wenn Sie es benötigen, können Sie darauf zurückgreifen Haben Sie gute Ideen? Sie können eine Nachricht hinterlassen. (Es wurde nicht gründlich getestet, aber das Problem sollte nicht schwerwiegend sein)

Das Folgende ist die alte Regel, fügen Sie den Code direkt ein:

(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=&#39;" + settings.style.header + "&#39;></li>");
      item.data("data", data);
      var header = $("<div class=&#39;" + settings.style.header_title + "&#39; data-accordion=&#39;" + data.id + "&#39;>" +
        "<i class=&#39;" + settings.style.icon_base + "" + data.icon + "&#39;></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=&#39;" + settings.style.icon_base + "" + settings.style.icon_collapse + "&#39;></i>");
        toggle.css({ "font-size": "1.4em", "position": "absolute", "top": "7px", "right": "7px" });
        header.append(toggle);
        var content = $("<ul class=&#39;" + settings.style.content + "&#39;></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=&#39;" + data.id + "&#39;]");
      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(&#39;accordion&#39;);
      });
    }
  }
  $.fn.accordion = function () {
    var method = arguments[0];
    var args = arguments;
    if (typeof (method) == &#39;object&#39; || !method) {
      method = methods.init;
    } else if (methods[method]) {
      method = methods[method];
      args = $.makeArray(arguments).slice(1);
    } else {
      $.error(&#39;Method &#39; + method + &#39; does not exist on tw.accordion&#39;);
      return this;
    }
    return method.apply(this, args);
  }
})(jQuery);
Nach dem Login kopieren
.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;
    }
Nach dem Login kopieren


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage