[英]Make dropdown close when clicking
I want to make the dropdown close when clicking anywhere in a document, currently it only closes when I click on the dropdown menu itself. 我想在单击文档中的任何位置时关闭下拉菜单,当前仅在单击下拉菜单本身时关闭。 Here's what I currently have of the HTML and JQuery:
这是我目前拥有的HTML和JQuery:
HTML: HTML:
<div class="user-menu">
<div id="dropdown" class="ddmenu">
<img src="#" class="user-menu-pic" alt="">
<span>Name Lastname</span>
<img src="images/dropdown-icon.png" class="right dropdown-icon" width="30" height="30" alt="">
<ul>
<li><a href="#">My Profile</a></li>
<li><a href="#">Change profile picture/background</a></li>
<li><a href="#">Settings</a></li>
<li><a href="#">Change password</a></li>
<li><a href="#">Sign Out</a></li>
</ul>
</div>
</div>
Jquery: jQuery的:
$('#dropdown').on('click', function (e) {
e.preventDefault();
if ($(this).hasClass('open')) {
$(this).removeClass('open');
$(this).children('ul').slideUp('fast');
} else {
$(this).addClass('open');
$(this).children('ul').slideDown('fast');
}
});
I think you are looking for something like this: 我认为您正在寻找这样的东西:
$('#dropdown').on('click', function (e) { e.preventDefault(); if ($(this).hasClass('open')) { $(this).removeClass('open'); $(this).children('ul').slideUp('fast'); } else { $(this).addClass('open'); $(this).children('ul').slideDown('fast'); } }); $(document).mouseup(function (e) { var container = $("#dropdown"); if (!container.is(e.target) // if the target of the click isn't the container... && container.has(e.target).length === 0) // ... nor a descendant of the container { container.children('ul').slideUp('fast'); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="user-menu"> <div id="dropdown" class="ddmenu"> <img src="#" class="user-menu-pic" alt=""> <span>Name Lastname</span> <img src="images/dropdown-icon.png" class="right dropdown-icon" width="30" height="30" alt=""> <ul> <li><a href="#">My Profile</a> </li> <li><a href="#">Change profile picture/background</a> </li> <li><a href="#">Settings</a> </li> <li><a href="#">Change password</a> </li> <li><a href="#">Sign Out</a> </li> </ul> </div> </div>
Note : Actually it doesn't need two clicks, it's working with very first click.
注意:实际上,不需要单击两次,只需单击一次即可使用。 But you can see that when you click on Menu(very first time), so it is already open(ie Already slideDown) and does not have class
open
.但是您可以看到,当您第一次单击Menu时,它已经打开(即Already slideDown)并且没有
open
类。 So it executes theloop
which returns false forhasClass("open")
.因此,它将执行为
hasClass("open")
返回false的loop
。 Now in that loop we say menu toslideDown
, which is already in theslideDown
State.现在在该循环中,我们说到
slideDown
菜单,它已经处于slideDown
状态。
If you want better explanation do me a favor add class="ddmenu open"
to your html like this: <div id="dropdown" class="ddmenu open">
and then try it again. 如果您想要更好的解释,请帮我一个忙,像这样在您的html中添加
class="ddmenu open"
: <div id="dropdown" class="ddmenu open">
,然后重试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.