简体   繁体   English

如果用户在下拉菜单之外单击,如何关闭下拉菜单?

[英]How to close the dropdown if the user clicks outside of it?

I am making user dropdown when we click on the picture we get a dropdown or a card.当我们点击图片时,我正在制作用户下拉菜单,我们会得到一个下拉菜单或一张卡片。 I want to close it when user click outside of dropdown.当用户在下拉菜单之外单击时,我想关闭它。 The code of closing will be in JavaScript.关闭代码将使用 JavaScript。 Please solve this problem.请解决这个问题。 I think there will be a remove function in JavaScript which will remove the dropdown after clicking on it.我认为 JavaScript 中会有一个删除功能,它会在点击下拉菜单后删除它。

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Simple popup menu</title>

<style>
    body {
        margin: 0;
        background: tomato;
        font-family: 'Open Sans', sans-serif;
    }

    .menubtn {
        cursor: pointer;
        width: 40px;
        height: 20px;
        right: 10px;
        top: 10px;
        position: absolute;
    }

    .userimg {
        border-radius: 50%;
        height: 35px;
        width: 35px;
    }

    .navmenu {
        width: 150px;
        border-radius: 10px;
        margin-top: 30px;
        background: #fff;
        position: absolute;
        right: 18px;
        top: 25px;
        box-shadow: 0 0 10px 2px rgba(0, 0, 0, 0.15);
        z-index: 10;
        visibility: hidden;
        opacity: 0;
        -webkit-transition: all 300ms ease;
        transition: all 300ms ease;
    }

    .navmenu.opened {
        visibility: visible;
        opacity: 1;
    }

    .navmenu::before {
        content: '';
        position: absolute;
        top: -5px;
        right: 7px;
        width: 15px;
        height: 15px;
        background: #fff;
        -webkit-transform: rotate(45deg);
        transform: rotate(45deg);
    }

    .navmenu ul {
        list-style: none;
        margin: 0;
        padding: 0;
    }

    .navmenu ul.text-list {
        text-align: left;
        font-weight: 100;
        width: 100%;
        margin: auto;
        padding-top: 10px;
        padding-bottom: 10px;
    }

    .navmenu ul.text-list li a {
        text-decoration: none;
        padding-left: 20px;
        padding-top: 5px;
        color: #343434;
        font-weight: 600;
        display: block;
        line-height: 27px;
        -webkit-transition: all 200ms ease;
        transition: all 200ms ease;
    }

    .navmenu ul.text-list li a:hover {
        color: tomato;
    }
</style>
</head>

<body>
<div class="menubtn">
    <span>
        <img class="userimg" src="http://tryma.in/images/user/default.png"/>
    </span>
</div>
<div class="navmenu">
    <ul class="text-list">
        <li><a href="#">Home</a></li>
        <li><a href="#">Gallery</a></li>
        <li><a href="#">Contact</a></li>
    </ul>
</div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
    $('.menubtn').on('click', function() {
        $('.navmenu').toggleClass('opened');
    });
</script>
</body>

</html>

Please help me in solving this code.请帮助我解决此代码。 Thank You谢谢你

  • Bind click on document and remove opened class on click of document.绑定单击文档并在单击文档时删除opened类。
  • Usee.stopPropagation() so that event does not propagate on document which event is invoked on click of the .menubtn使用e.stopPropagation()以便事件不会在单击.menubtn调用哪个事件的document上传播

 $('.menubtn').on('click', function(e) { e.stopPropagation(); $('.navmenu').toggleClass('opened'); }); $(document).on('click', function() { $('.navmenu').removeClass('opened'); });
 body { margin: 0; background: tomato; font-family: 'Open Sans', sans-serif; } .menubtn { cursor: pointer; width: 40px; height: 20px; right: 10px; top: 10px; position: absolute; } .userimg { border-radius: 50%; height: 35px; width: 35px; } .navmenu { width: 150px; border-radius: 10px; margin-top: 30px; background: #fff; position: absolute; right: 18px; top: 25px; box-shadow: 0 0 10px 2px rgba(0, 0, 0, 0.15); z-index: 10; visibility: hidden; opacity: 0; -webkit-transition: all 300ms ease; transition: all 300ms ease; } .navmenu.opened { visibility: visible; opacity: 1; } .navmenu::before { content: ''; position: absolute; top: -5px; right: 7px; width: 15px; height: 15px; background: #fff; -webkit-transform: rotate(45deg); transform: rotate(45deg); } .navmenu ul { list-style: none; margin: 0; padding: 0; } .navmenu ul.text-list { text-align: left; font-weight: 100; width: 100%; margin: auto; padding-top: 10px; padding-bottom: 10px; } .navmenu ul.text-list li a { text-decoration: none; padding-left: 20px; padding-top: 5px; color: #343434; font-weight: 600; display: block; line-height: 27px; -webkit-transition: all 200ms ease; transition: all 200ms ease; } .navmenu ul.text-list li a:hover { color: tomato; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="menubtn"> <span> <img class="userimg" src="http://tryma.in/images/user/default.png"/> </span> </div> <div class="navmenu"> <ul class="text-list"> <li><a href="#">Home</a></li> <li><a href="#">Gallery</a></li> <li><a href="#">Contact</a></li> </ul> </div>

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

相关问题 如果用户在下拉菜单之外单击,则关闭下拉菜单 - Close the dropdown if the user clicks outside of it 用户在菜单外单击时如何关闭Bootstrap导航栏下拉菜单? - How to close the Bootstrap navbar dropdown when the user clicks outside the menu? 用户点击时如何关闭下拉菜单 - How to close dropdown menu when user clicks 当用户在下拉菜单之外单击时如何隐藏下拉菜单 - How to hide a dropdown when the user clicks outside of it 当用户在外部单击时,为什么添加第二个下拉菜单会使 JS 关闭菜单变得混乱? - Why does adding a second dropdown mess up the JS to close menu when user clicks outside it? 当用户在其外部单击时关闭模式,为什么有==而不是!=? - Close modal when user clicks outside of it, why is there == and not !=? 当用户在模态外单击时关闭模态 - Close the modal when user clicks outside the modal 如果用户单击第二个下拉链接,则关闭下拉菜单 - Close the dropdown menu if the user clicks on the second dropdown link 当用户点击模态窗口之外时,如何使用jquery关闭w3schools示例中的模态窗口? - How to use jquery to close the modal window in the w3schools example when user clicks outside of modal window? 当用户单击页面上的其他任何位置时如何关闭下拉菜单 - How to get dropdown menu to close when user clicks anywhere else on the page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM