选择选项后,如何使此下拉菜单不会自行关闭

时间:2015-03-25 10:39:09

标签: javascript jquery html twitter-bootstrap drop-down-menu

我正在使用这个包含下拉菜单的NavBar:

http://jsfiddle.net/rvetgyen/

/**
 * Created by Kupletsky Sergey on 17.10.14.
 *
 * Material Sidebar (Profile menu)
 * Tested on Win8.1 with browsers: Chrome 37, Firefox 32, Opera 25, IE 11, Safari 5.1.7
 * You can use this sidebar in Bootstrap (v3) projects. HTML-markup like Navbar bootstrap component will make your work easier.
 * Dropdown menu and sidebar toggle button works with JQuery and Bootstrap.min.js
 */

// Sidebar toggle
//
// -------------------
$(document).ready(function() {
  var overlay = $('.sidebar-overlay');

  $('.sidebar-toggle').on('click', function() {
    var sidebar = $('#sidebar');
    sidebar.toggleClass('open');
    if ((sidebar.hasClass('sidebar-fixed-left') || sidebar.hasClass('sidebar-fixed-right')) && sidebar.hasClass('open')) {
      overlay.addClass('active');
    } else {
      overlay.removeClass('active');
    }
  });

  overlay.on('click', function() {
    $(this).removeClass('active');
    $('#sidebar').removeClass('open');
  });

});

// Sidebar constructor
//
// -------------------
$(document).ready(function() {

  var sidebar = $('#sidebar');
  var sidebarHeader = $('#sidebar .sidebar-header');
  var sidebarImg = sidebarHeader.css('background-image');
  var toggleButtons = $('.sidebar-toggle');

  // Hide toggle buttons on default position
  toggleButtons.css('display', 'none');
  $('body').css('display', 'table');


  // Sidebar position
  $('#sidebar-position').change(function() {
    var value = $(this).val();
    sidebar.removeClass('sidebar-fixed-left sidebar-fixed-right sidebar-stacked').addClass(value).addClass('open');
    if (value == 'sidebar-fixed-left' || value == 'sidebar-fixed-right') {
      $('.sidebar-overlay').addClass('active');
    }
    // Show toggle buttons
    if (value != '') {
      toggleButtons.css('display', 'initial');
      $('body').css('display', 'initial');
    } else {
      // Hide toggle buttons
      toggleButtons.css('display', 'none');
      $('body').css('display', 'table');
    }
  });

  // Sidebar theme
  $('#sidebar-theme').change(function() {
    var value = $(this).val();
    sidebar.removeClass('sidebar-default sidebar-inverse sidebar-colored sidebar-colored-inverse').addClass(value)
  });

  // Header cover
  $('#sidebar-header').change(function() {
    var value = $(this).val();

    $('.sidebar-header').removeClass('header-cover').addClass(value);

    if (value == 'header-cover') {
      sidebarHeader.css('background-image', sidebarImg)
    } else {
      sidebarHeader.css('background-image', '')
    }
  });
});

/**
 * Created by Kupletsky Sergey on 08.09.14.
 *
 * Add JQuery animation to bootstrap dropdown elements.
 */

(function($) {
  var dropdown = $('.dropdown');

  // Add slidedown animation to dropdown
  dropdown.on('show.bs.dropdown', function(e) {
    $(this).find('.dropdown-menu').first().stop(true, true).slideDown();
  });

  // Add slideup animation to dropdown
  dropdown.on('hide.bs.dropdown', function(e) {
    $(this).find('.dropdown-menu').first().stop(true, true).slideUp();
  });
})(jQuery);



(function(removeClass) {

  jQuery.fn.removeClass = function(value) {
    if (value && typeof value.test === "function") {
      for (var i = 0, l = this.length; i < l; i++) {
        var elem = this[i];
        if (elem.nodeType === 1 && elem.className) {
          var classNames = elem.className.split(/\s+/);

          for (var n = classNames.length; n--;) {
            if (value.test(classNames[n])) {
              classNames.splice(n, 1);
            }
          }
          elem.className = jQuery.trim(classNames.join(" "));
        }
      }
    } else {
      removeClass.call(this, value);
    }
    return this;
  }

})(jQuery.fn.removeClass);
@import"https://fonts.googleapis.com/css?family=Roboto:400,100,100italic,300,300italic,400italic,500,500italic,700,700italic,900,900italic&subset=latin,cyrillic";

/* -- import Roboto Font ---------------------------- */

/* -- import Material Icons Font -------------------- */

@font-face {
  font-family: 'Material Design Iconic Font';
  src: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/53474/Material-Design-Iconic-Font.eot?v=1.0');
  src: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/53474/Material-Design-Iconic-Font.eot?#iefix&v=1.0') format('embedded-opentype'), url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/53474/Material-Design-Iconic-Font.woff?v=1.0') format('woff'), url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/53474/Material-Design-Iconic-Font.ttf?v=1.0') format('truetype'), url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/53474/Material-Design-Iconic-Font.svg?v=1.0#Material-Design-Iconic-Font') format('svg');
  font-weight: normal;
  font-style: normal;
}
[class^="mds-"],
[class*=" mds-"] {
  display: inline-block;
  font: normal normal normal 14px/1'Material Design Iconic Font';
  font-size: inherit;
  speak: none;
  text-rendering: auto;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
.mds {
  line-height: inherit;
  vertical-align: bottom;
}
.mds-inbox:before {
  content: "\f10c";
}
.mds-star:before {
  content: "\f2e5";
}
.mds-send:before {
  content: "\f119";
}
.mds-drafts:before {
  content: "\f107";
}
.mds-arrow-back:before {
  content: "\f297";
}
.mds-arrow-forward:before {
  content: "\f298";
}
/* -- You can use this sidebar in Bootstrap (v3) projects. HTML-markup like Navbar bootstrap component will make your work easier. -- */

/* -- Box model ------------------------------- */

*,
*:after,
*:before {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
/* -- Demo style ------------------------------- */

html,
body {
  position: relative;
  min-height: 100%;
  height: 100%;
}
body {
  font-family: 'RobotoDraft', 'Roboto', 'Helvetica Neue, Helvetica, Arial', sans-serif;
  font-style: normal;
  font-weight: 300;
  font-size: 14px;
  line-height: 1.4;
  color: #212121;
  background-color: #f5f5f5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}
.sidebar-overlay {
  visibility: hidden;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0;
  background: #000;
  z-index: 1034;
  -webkit-transition: visibility 0 linear 0.4s, opacity 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  -moz-transition: visibility 0 linear 0.4s, opacity 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  transition: visibility 0 linear 0.4s, opacity 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  -webkit-transform: translateZ(0);
  -moz-transform: translateZ(0);
  -ms-transform: translateZ(0);
  -o-transform: translateZ(0);
  transform: translateZ(0);
}
.sidebar-overlay.active {
  opacity: 0.5;
  visibility: visible;
  -webkit-transition-delay: 0;
  -moz-transition-delay: 0;
  transition-delay: 0;
}
.top-bar {
  height: 25px;
  background: rgba(0, 0, 0, 0.1);
}
/* -- Google typography ------------------------------- */

.headline {
  font-size: 24px;
  font-weight: 300;
  line-height: 1.1;
  color: #212121;
  text-transform: inherit;
  letter-spacing: inherit;
}
.subhead {
  font-size: 16px;
  font-weight: 300;
  line-height: 1.1;
  color: #212121;
  text-transform: inherit;
  letter-spacing: inherit;
}
/* -- Bootstrap-like style ------------------------------- */

.caret {
  display: inline-block;
  width: 0;
  height: 0;
  margin-left: 2px;
  vertical-align: middle;
  border-top: 4px solid;
  border-right: 4px solid transparent;
  border-left: 4px solid transparent;
}
.dropdown-menu {
  display: none;
}
/* -- Constructor style ------------------------------- */

.constructor {
  position: relative;
  margin: 0;
  padding: 0 0px;
  -webkit-transition: all 0.5s cubic-bezier(0.55, 0, 0.1, 1);
  -o-transition: all 0.5s cubic-bezier(0.55, 0, 0.1, 1);
  transition: all 0.5s cubic-bezier(0.55, 0, 0.1, 1);
}
.sidebar,
.wrapper {
  display: table-cell;
  vertical-align: top;
}
.sidebar-stacked.open + .wrapper .constructor {
  margin-left: 280px;
}
@media (max-width: 768px) {
  .sidebar-stacked.open + .wrapper .constructor {
    margin-left: 240px;
  }
}
/* -- Sidebar style ------------------------------- */

.sidebar {
  position: relative;
  display: block;
  min-height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  border: none;
  -webkit-transition: all 0.5s cubic-bezier(0.55, 0, 0.1, 1);
  -o-transition: all 0.5s cubic-bezier(0.55, 0, 0.1, 1);
  transition: all 0.5s cubic-bezier(0.55, 0, 0.1, 1);
}
.sidebar:before,
.sidebar:after {
  content: " ";
  display: table;
}
.sidebar:after {
  clear: both;
}
.sidebar::-webkit-scrollbar-track {
  border-radius: 2px;
}
.sidebar::-webkit-scrollbar {
  width: 5px;
  background-color: #F7F7F7;
}
.sidebar::-webkit-scrollbar-thumb {
  border-radius: 10px;
  -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
  background-color: #BFBFBF;
}
.sidebar .sidebar-header {
  position: relative;
  height: 157.5px;
  margin-bottom: 8px;
  -webkit-transition: all 0.2s ease-in-out;
  -o-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
}
.sidebar .sidebar-header.header-cover {
  background-repeat: no-repeat;
  background-position: center center;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}
.sidebar .sidebar-header:hover .sidebar-toggle {
  opacity: 1;
}
.sidebar .sidebar-toggle {
  position: relative;
  float: right;
  margin: 16px;
  padding: 0;
  background-image: none;
  border: none;
  height: 40px;
  width: 40px;
  font-size: 20px;
  opacity: 0.7;
  -webkit-transition: all 0.2s ease-in-out;
  -o-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
}
.sidebar .sidebar-toggle:before,
.sidebar .sidebar-toggle:after {
  content: " ";
  display: table;
}
.sidebar .sidebar-toggle:after {
  clear: both;
}
.sidebar .icon-material-sidebar-arrow:before {
  content: "\e610";
}
.sidebar .sidebar-image img {
  width: 54px;
  height: 54px;
  margin: 16px;
  border-radius: 50%;
  -webkit-transition: all 0.2s ease-in-out;
  -o-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
}
.sidebar .sidebar-brand {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  display: block;
  height: 48px;
  line-height: 48px;
  padding: 0;
  padding-left: 16px;
  padding-right: 56px;
  text-decoration: none;
  clear: both;
  font-weight: 500;
  overflow: hidden;
  -o-text-overflow: ellipsis;
  text-overflow: ellipsis;
  white-space: nowrap;
  -webkit-transition: all 0.2s ease-in-out;
  -o-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
}
.sidebar .sidebar-brand:hover,
.sidebar .sidebar-brand:focus {
  -webkit-box-shadow: none;
  box-shadow: none;
  outline: none;
}
.sidebar .sidebar-brand .caret {
  position: absolute;
  right: 24px;
  top: 24px;
}
.sidebar .sidebar-brand .sidebar-badge {
  position: absolute;
  right: 16px;
  top: 12px;
}
.sidebar .sidebar-brand:hover,
.sidebar .sidebar-brand:focus {
  text-decoration: none;
}
.sidebar .sidebar-badge {
  display: inline-block;
  min-width: 24px;
  height: 24px;
  line-height: 24px;
  padding: 0 3px;
  font-size: 10px;
  text-align: center;
  white-space: nowrap;
  vertical-align: baseline;
}
.sidebar .sidebar-badge.badge-circle {
  border-radius: 50%;
}
.sidebar .sidebar-divider,
.sidebar .sidebar-nav .divider {
  position: relative;
  display: block;
  height: 1px;
  margin: 8px 0;
  padding: 0;
  overflow: hidden;
}
.sidebar .sidebar-text {
  display: block;
  height: 48px;
  line-height: 48px;
  padding: 0;
  padding-left: 16px;
  padding-right: 56px;
  text-decoration: none;
  clear: both;
  font-weight: 500;
  overflow: hidden;
  -o-text-overflow: ellipsis;
  text-overflow: ellipsis;
  white-space: nowrap;
  -webkit-transition: all 0.2s ease-in-out;
  -o-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
}
.sidebar .sidebar-text:hover,
.sidebar .sidebar-text:focus {
  -webkit-box-shadow: none;
  box-shadow: none;
  outline: none;
}
.sidebar .sidebar-text .caret {
  position: absolute;
  right: 24px;
  top: 24px;
}
.sidebar .sidebar-text .sidebar-badge {
  position: absolute;
  right: 16px;
  top: 12px;
}
.sidebar .sidebar-icon {
  display: inline-block;
  margin-right: 16px;
  min-width: 40px;
  width: 40px;
  text-align: left;
  font-size: 20px;
}
.sidebar .sidebar-icon:before,
.sidebar .sidebar-icon:after {
  vertical-align: middle;
}
.sidebar .sidebar-nav {
  margin: 0;
  padding: 0;
}
.sidebar .sidebar-nav li {
  position: relative;
  list-style-type: none;
}
.sidebar .sidebar-nav li a {
  position: relative;
  cursor: pointer;
  user-select: none;
  display: block;
  height: 48px;
  line-height: 48px;
  padding: 0;
  padding-left: 16px;
  padding-right: 56px;
  text-decoration: none;
  clear: both;
  font-weight: 500;
  overflow: hidden;
  -o-text-overflow: ellipsis;
  text-overflow: ellipsis;
  white-space: nowrap;
  -webkit-transition: all 0.2s ease-in-out;
  -o-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-in-out;
}
.sidebar .sidebar-nav li a:hover,
.sidebar .sidebar-nav li a:focus {
  -webkit-box-shadow: none;
  box-shadow: none;
  outline: none;
}
.sidebar .sidebar-nav li a .caret {
  position: absolute;
  right: 24px;
  top: 24px;
}
.sidebar .sidebar-nav li a .sidebar-badge {
  position: absolute;
  right: 16px;
  top: 12px;
}
.sidebar .sidebar-nav li a:hover {
  background: transparent;
}
.sidebar .sidebar-nav .dropdown-menu {
  position: relative;
  width: 100%;
  margin: 0;
  padding: 0;
  border: none;
  border-radius: 0;
  -webkit-box-shadow: none;
  box-shadow: none;
}
.sidebar-default {
  background-color: #ffffff;
}
.sidebar-default .sidebar-header {
  background-color: #eceff1;
}
.sidebar-default .sidebar-toggle {
  color: #212121;
  background-color: transparent;
}
.sidebar-default .sidebar-brand {
  color: #DEDEDE;
  background-color: transparent;
}
.sidebar-default .sidebar-brand:hover,
.sidebar-default .sidebar-brand:focus {
  color: #212121;
  background-color: rgba(0, 0, 0, 0.1);
}
.sidebar-default .sidebar-badge {
  color: #ffffff;
  background-color: #bdbdbd;
}
.sidebar-default .sidebar-divider,
.sidebar-default .sidebar-nav .divider {
  background-color: #bdbdbd;
}
.sidebar-default .sidebar-text {
  color: #212121;
}
.sidebar-default .sidebar-nav li > a {
  color: #212121;
  background-color: transparent;
}
.sidebar-default .sidebar-nav li > a i {
  color: #757575;
}
.sidebar-default .sidebar-nav li:hover > a,
.sidebar-default .sidebar-nav li > a:hover {
  color: #212121;
  background-color: #e0e0e0;
}
.sidebar-default .sidebar-nav li:hover > a i,
.sidebar-default .sidebar-nav li > a:hover i {
  color: #757575;
}
.sidebar-default .sidebar-nav li:focus > a,
.sidebar-default .sidebar-nav li > a:focus {
  color: #212121;
  background-color: transparent;
}
.sidebar-default .sidebar-nav li:focus > a i,
.sidebar-default .sidebar-nav li > a:focus i {
  color: #757575;
}
.sidebar-default .sidebar-nav > .open > a,
.sidebar-default .sidebar-nav > .open > a:hover,
.sidebar-default .sidebar-nav > .open > a:focus {
  color: #212121;
  background-color: #e0e0e0;
}
.sidebar-default .sidebar-nav > .active > a,
.sidebar-default .sidebar-nav > .active > a:hover,
.sidebar-default .sidebar-nav > .active > a:focus {
  color: #212121;
  background-color: #e0e0e0;
}
.sidebar-default .sidebar-nav > .disabled > a,
.sidebar-default .sidebar-nav > .disabled > a:hover,
.sidebar-default .sidebar-nav > .disabled > a:focus {
  color: #e0e0e0;
  background-color: transparent;
}
.sidebar-default .sidebar-nav > .dropdown > .dropdown-menu {
  background-color: #e0e0e0;
}
.sidebar-default .sidebar-nav > .dropdown > .dropdown-menu > li > a:focus {
  background-color: #e0e0e0;
  color: #212121;
}
.sidebar-default .sidebar-nav > .dropdown > .dropdown-menu > li > a:hover {
  background-color: #cecece;
  color: #212121;
}
.sidebar-default .sidebar-nav > .dropdown > .dropdown-menu > .active > a,
.sidebar-default .sidebar-nav > .dropdown > .dropdown-menu > .active > a:hover,
.sidebar-default .sidebar-nav > .dropdown > .dropdown-menu > .active > a:focus {
  color: #212121;
  background-color: #e0e0e0;
}
.sidebar {
  width: 0;
  -webkit-transform: translate3d(-280px, 0, 0);
  transform: translate3d(-280px, 0, 0);
}
.sidebar.open {
  min-width: 280px;
  width: 280px;
  -webkit-transform: translate3d(0, 0, 0);
  transform: translate3d(0, 0, 0);
}
.sidebar-fixed-left,
.sidebar-fixed-right,
.sidebar-stacked {
  position: fixed;
  top: 0;
  bottom: 0;
  z-index: 1035;
}
.sidebar-stacked {
  left: 0;
}
.sidebar-fixed-left {
  left: 0;
  box-shadow: 2px 0px 15px rgba(0, 0, 0, 0.35);
  -webkit-box-shadow: 2px 0px 15px rgba(0, 0, 0, 0.35);
}
.sidebar-fixed-right {
  right: 0;
  box-shadow: 0px 2px 15px rgba(0, 0, 0, 0.35);
  -webkit-box-shadow: 0px 2px 15px rgba(0, 0, 0, 0.35);
  -webkit-transform: translate3d(280px, 0, 0);
  transform: translate3d(280px, 0, 0);
}
.sidebar-fixed-right.open {
  -webkit-transform: translate3d(0, 0, 0);
  transform: translate3d(0, 0, 0);
}
.sidebar-fixed-right .icon-material-sidebar-arrow:before {
  content: "\e614";
}
@media (max-width: 768px) {
  .sidebar.open {
    min-width: 240px;
    width: 240px;
  }
  .sidebar .sidebar-header {
    height: 135px;
  }
  .sidebar .sidebar-image img {
    width: 44px;
    height: 44px;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Overlay for fixed sidebar -->
<div class="sidebar-overlay"></div>
<!-- Material sidebar -->
<aside id="sidebar" class="sidebar sidebar-default open" role="navigation">
  <!-- Sidebar header -->
  <div class="sidebar-header header-cover" style="background-image: url(http://2.bp.blogspot.com/-2RewSLZUzRg/U-9o6SD4M6I/AAAAAAAADIE/voax99AbRx0/s1600/14%2B-%2B1%2B%281%29.jpg);">
    <!-- Top bar -->
    <div class="top-bar"></div>
    <!-- Sidebar toggle button -->
    <button type="button" class="sidebar-toggle"> <i class="icon-material-sidebar-arrow"></i>

    </button>
    <!-- Sidebar brand image -->
    <div class="sidebar-image">
      <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/53474/atom_profile_01.jpg">
    </div>
    <!-- Sidebar brand name --> <a data-toggle="dropdown" class="sidebar-brand" href="#settings-dropdown">
            john.doe@gmail.com
            <b class="caret"></b>
        </a>

  </div>
  <!-- Sidebar navigation -->
  <ul class="nav sidebar-nav">
    <li class="dropdown">
      <ul id="settings-dropdown" class="dropdown-menu">
        <li> <a href="#" tabindex="-1">
                        Profile
                    </a>

        </li>
        <li> <a href="#" tabindex="-1">
                        Settings
                    </a>

        </li>
        <li> <a href="#" tabindex="-1">
                        Help
                    </a>

        </li>
        <li> <a href="#" tabindex="-1">
                        Exit
                    </a>

        </li>
      </ul>
    </li>
    <li>
      <a href="#">
        <i class="sidebar-icon md-inbox"></i>
        Inbox
      </a>

    </li>
    <li>
      <a href="#">
        <i class="sidebar-icon md-star"></i>
        Starred
      </a>

    </li>
    <li>
      <a href="#">
        <i class="sidebar-icon md-send"></i>
        Sent Mail
      </a>

    </li>
    <li>
      <a href="#">
        <i class="sidebar-icon md-drafts"></i>
        Drafts
      </a>

    </li>
    <li class="divider"></li>
    <li class="dropdown"> <a class="ripple-effect dropdown-toggle" href="#" data-toggle="dropdown">
                All Mail
                <b class="caret"></b>
            </a>

      <ul class="dropdown-menu">
        <li> <a href="#" tabindex="-1">
                        Social
                        <span class="sidebar-badge">12</span>
                    </a>

        </li>
        <li> <a href="#" tabindex="-1">
                        Promo
                        <span class="sidebar-badge">0</span>
                    </a>

        </li>
      </ul>
    </li>
    <li> <a href="#">
                Trash
                <span class="sidebar-badge">3</span>
            </a>

    </li>
    <li> <a href="#">
                Spam
                <span class="sidebar-badge">456</span>
            </a>

    </li>
    <li> <a href="#">
                Follow Up
                <span class="sidebar-badge badge-circle">i</span>
            </a>

    </li>
  </ul>
  <!-- Sidebar divider -->
  <!-- <div class="sidebar-divider"></div> -->
  <!-- Sidebar text -->
  <!-- <div class="sidebar-text">Text</div> -->
</aside>

我想以这样的方式制作:如果您单击“所有邮件”然后单击“社交”,“所有邮件”下拉列表仍将打开,关闭它的唯一方法是再次单击“所有邮件“。

为了更好地说明我想要实现的目标,结果类似于这个侧边栏:

https://material.angularjs.org/#/

选择一个类别后,您可以浏览元素和下拉列表(如果我可以这样调用它),请不要隐藏。

1 个答案:

答案 0 :(得分:0)

这将解决您的问题:

$("ul.dropdown-menu li a").click(function(){
     $(this).parent().parent().siblings(".dropdown-toggle").show();
});