编程语言
首页 > 编程语言> > javascript – Bootstrap Horizo​​ntal Nav加手风琴

javascript – Bootstrap Horizo​​ntal Nav加手风琴

作者:互联网

我正在尝试根据需要调整脚本.
这是一个显示手风琴的水平导航菜单.

一切都很好,但问题是我希望手风琴关闭onclick事件到其子菜单之一.

$(function() {
  $('.dropdown-menu').on('click', function(e) {
    e.stopPropagation();
  });
  var Accordion = function(el, multiple) {
    this.el = el || {};
    this.multiple = multiple || false;

    // Variables privadas
    var links = this.el.find('.link');
    // Evento
    links.on('click', {
      el: this.el,
      multiple: this.multiple
    }, this.dropdown)
  }

  Accordion.prototype.dropdown = function(e) {
    var $el = e.data.el;
    $this = $(this),
      $next = $this.next();

    $next.slideToggle();
    $this.parent().toggleClass('open');

    if (!e.data.multiple) {
      $el.find('.submenu').not($next).slideUp().parent().removeClass('open');
    };
  }

  var accordion = new Accordion($('#accordion'), false);
  $(".cls-menu").on('click', function() {
    $('.dropdown-menu').dropdown('toggle');
  });
});
.accordion {
  width: 100%;
  max-width: 360px;
  padding: 0px;
  background: #FFF;
  color: #000000;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
}

.accordion .link {
  cursor: pointer;
  display: block;
  padding: 15px 15px 15px 42px;
  color: #000000;
  font-size: 14px;
  font-weight: 700;
  border-bottom: 1px solid #CCC;
  position: relative;
  -webkit-transition: all 0.4s ease;
  -o-transition: all 0.4s ease;
  transition: all 0.4s ease;
}

.accordion li:last-child .link {
  border-bottom: 0;
}

.accordion li i {
  position: absolute;
  top: 16px;
  left: 12px;
  font-size: 18px;
  color: #000000;
  -webkit-transition: all 0.4s ease;
  -o-transition: all 0.4s ease;
  transition: all 0.4s ease;
}

.accordion li i.fa-chevron-down {
  right: 12px;
  left: auto;
  font-size: 16px;
}

.accordion li.open .link {
  color: #000000;
}

.accordion li.open i {
  color: #000000;
}

.accordion li.open i.fa-chevron-down {
  -webkit-transform: rotate(180deg);
  -ms-transform: rotate(180deg);
  -o-transform: rotate(180deg);
  transform: rotate(180deg);
}


/**
 * Submenu
 -----------------------------*/

.submenu {
  padding: 0px;
  display: none;
  background: #FFF;
  font-size: 14px;
}

.submenu li {
  border-bottom: 1px solid #FFF;
}

.submenu a {
  display: block;
  text-decoration: none;
  color: #000000;
  padding: 12px;
  padding-left: 42px;
  -webkit-transition: all 0.25s ease;
  -o-transition: all 0.25s ease;
  transition: all 0.25s ease;
}

.submenu a:hover {
  background: #FFF;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>



<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown">Departamentos<span class="caret"></span></a>
  <ul class="dropdown-menu pull-right">
    <ul id="accordion" class="accordion">
      <li>
        <div class="link"><i class="fa fa-database"></i>Web Design<i class="fa fa-chevron-down"></i></div>
        <ul class="submenu">
          <li><a href="#" class="cls-menu">Photoshop</a></li>
          <li><a href="#" class="cls-menu">HTML</a></li>
          <li><a href="#" class="cls-menu">CSS</a></li>
        </ul>
      </li>
      <li>
        <div class="link"><i class="fa fa-code"></i>Coding<i class="fa fa-chevron-down"></i></div>
        <ul class="submenu">
          <li><a href="#" class="cls-menu">Javascript</a></li>
          <li><a href="#" class="cls-menu">jQuery</a></li>
          <li><a href="#" class="cls-menu">Ruby</a></li>
        </ul>
      </li>
    </ul>
  </ul>
</li>

链接:https://jsfiddle.net/k3uzL58c/6/

解决方法:

解决了:

function HtmlMenu() {
    $('.dropdown-menu').on('click', function(e) {
        e.stopPropagation();
    });
    var Accordion = function(el, multiple) {
        this.el = el || {};
        this.multiple = multiple || false;

        // Variables privadas
        var links = this.el.find('.link');
        // Evento
        links.on('click', { el: this.el, multiple: this.multiple }, this.dropdown)
    }
    Accordion.prototype.dropdown = function(e) {
        var $el = e.data.el;
        $this = $(this),
            $next = $this.next();

        $next.slideToggle();
        $this.parent().toggleClass('open');

        if (!e.data.multiple) {
            $el.find('.submenu').not($next).slideUp().parent().removeClass('open');
        };
    }
    var accordion = new Accordion($('#accordion'), false);
    $("a.cls-menu").on("click", function() {
        $(".dropdown-toggle").dropdown("toggle");
    });
}

标签:jquery,javascript,twitter-bootstrap,accordion,bootstrap-4
来源: https://codeday.me/bug/20190701/1347096.html