简体   繁体   English

Jquery下拉菜单单击始终打开

[英]Jquery dropdown menu click always open

I created a dropdown menu click, but it have a little weirdness. 我创建了一个下拉菜单点击,但它有点奇怪。 When I click the button dropdown, the dropdown menu has appear. 单击按钮下拉列表时,会出现下拉菜单。 But when I move my cursor to another (without click the button dropdown again), the dropdown menu dissapear and it has become hoverable dropdown menu not dropdown menu click (Sorry for my bad English) 但是当我将光标移动到另一个时(没有再点击按钮下拉菜单),下拉菜单消失,它已成为可下降的下拉菜单而不是下拉菜单点击(抱歉我的英文不好)

在此输入图像描述

在此输入图像描述

在此输入图像描述

How can I make the dropdown menu click always appear when I click the button dropdown and move the cursor? 当我单击按钮下拉菜单并移动光标时,如何使下拉菜单单击始终显示?

(Here is my code) (这是我的代码)

HTML HTML

<aside class="sidebar">
      <ul>
        <li><a href="#"><i class="material-icons">home</i>Homepage</a></li>
        <li class="button_dropdown"><a href="javascript:void(0)" class="dropdown-toggle"><i class="material-icons">widgets</i>Events Organizer <i class="material-icons multi_menu">keyboard_arrow_right</i></a>
          <ul class="dropdown_menu">
            <li><a href="#">Create Events</a></li>
            <li><a href="#">List Events</a></li>
          </ul>
        </li>
        <li><a href="#"><i class="material-icons">people</i>Peserta Events</a></li>
      </ul>
    </aside>

CSS CSS

aside.sidebar ul li ul.dropdown_menu {
  opacity: 0;
  visibility: hidden;
  height: auto;
  width: 100%;
  margin-top: -1px;
  position: absolute;
  transition: all 0.5s;
  border-left: 1px solid #2c3e50;
  background-color: #34495e;
}
aside.sidebar ul li ul.dropdown_menu.active {
  opacity: 1;
  visibility: visible;
  height: auto;
  width: 100%;
  background-color: #34495e;
  left: 100%;
  top: 0;
  transition: all 0.5s;
}

Jquery jQuery的

  $(document).ready(function () {
        $(".button_dropdown").click(function () {
          $(".dropdown_menu").toggleClass("active");
        });
      });

Without changing your code too much, you can just remove the pointer-events (clicks, etc.) by adding: 在不更改代码的情况下,您可以通过添加以下内容来删除指针事件(点击等)。

pointer-events:none; to aside.sidebar ul li ul.dropdown_menu to aside.sidebar ul li ul.dropdown_menu

and

pointer-events:auto; to aside.sidebar ul li ul.dropdown_menu.active to aside.sidebar ul li ul.dropdown_menu.active

The hoverable dropdown menu is because you have set the opacity property to 0 in your css aside (dropdown_menu). 可停滞的下拉菜单是因为你已经在你的css中将opacity属性设置为0(dropdown_menu)。 You must change opacity:0 to opacity:1. 您必须更改不透明度:0到不透明度:1。 Here is your code with error: 这是您的错误代码:

  aside.sidebar ul li ul.dropdown_menu {
  opacity: 0;
  visibility: hidden;
  height: auto;
  width: 100%;
  margin-top: -1px;
  position: absolute;
  transition: all 0.5s;
  border-left: 1px solid #2c3e50;
  background-color: #34495e;
}

Replace by (fixed opacity): 替换为(固定不透明度):

 aside.sidebar ul li ul.dropdown_menu {
  opacity: 1;
  visibility: hidden;
  height: auto;
  width: 100%;
  margin-top: -1px;
  position: absolute;
  transition: all 0.5s;
  border-left: 1px solid #2c3e50;
  background-color: #34495e;
}

I personally would use hover rather than click for a child menu. 我个人会使用hover而不是click子菜单。 Let me know how you go with this. 让我知道你如何使用它。 Stays active until clicked out. 在点击之前保持活动状态。

aside.sidebar ul li ul.dropdown_menu {
  display: none;
  height: auto;
  width: 100%;
  margin-top: -1px;
  position: absolute;
  transition: all 0.5s;
  border-left: 1px solid #2c3e50;
  background-color: #34495e;
  left:200px;
  top:0;
}

aside.sidebar ul li ul.dropdown_menu.active {
  display: block !important;
}

Working in this snippet. 使用此代码段。

 $(document).ready(function() { $('.button_dropdown').click(function() { $('.dropdown_menu').toggleClass('active'); }); }); 
 aside.sidebar ul li ul.dropdown_menu { display: none; height: auto; width: 100%; margin-top: -1px; position: absolute; transition: all 0.5s; border-left: 1px solid #2c3e50; background-color: #34495e; left:200px; top:0; } aside.sidebar ul li ul.dropdown_menu.active { display: block !important; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <aside class="sidebar"> <ul> <li>Homepage</li> <li class="button_dropdown"><a href="javascript:void(0)" class="dropdown-toggle">Events Organizer</a> <ul class="dropdown_menu"> <li>Create Events</li> <li>List Events</li> </ul> </li> <li>Peserta Events</li> </ul> </aside> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM