简体   繁体   English

单击菜单以外的任何位置时隐藏菜单

[英]Hide menu, when clicking anywhere else than the menu

I have a menu that appears when I click an icon. 点击图标后会出现一个菜单。 Currently I can close it by clicking the 'close' icon, but I would like to be able to close it by clicking anywhere outside of the menu, when the menu is visible. 目前我可以点击“关闭”图标关闭它,但我希望能够通过点击菜单外的任何地方关闭它,当菜单可见时。

Here is a jsFiddle: http://jsfiddle.net/budapesti/3v5ym2bp/3/ 这是一个jsFiddle: http//jsfiddle.net/budapesti/3v5ym2bp/3/

For example the following doesn't work: 例如,以下不起作用:

$(document).click(function() { 
        if($('.menu').is(":visible")) {
            $('.menu').hide()
        }      
});

I found similar questions, such as jQuery: Hide element on click anywhere else apart of the element and How do I detect a click outside an element? 我发现了类似的问题,例如jQuery:在元素的其他任何地方点击隐藏元素如何检测元素外的点击? , but couldn't get the solutions to work for me. ,但无法让解决方案为我工作。


EDIT: I wonder if is(":visible") works with jQuery "animate"? 编辑:我想知道是否(“:可见”)与jQuery“animate”一起使用?

call close function on anywhere click on window. 在任何地方点击窗口调用关闭功能。 And use event bubbling. 并使用事件冒泡。 i had update it its jsfiddle link is 我更新了它的jsfiddle链接

> http://jsfiddle.net/rahulrchaurasia/3v5ym2bp/19/

Try this : http://jsfiddle.net/3v5ym2bp/13/ 试试这个: http//jsfiddle.net/3v5ym2bp/13/

html HTML

<div class="inv"></div><!-- Added an invisible block -->
<div class="menu"> <span class="glyphicon glyphicon-remove closed pull-right"></span>

    <ul>
        <li>Link</li>
        <li>Link</li>
        <li>Link</li>
    </ul>
</div>
<div class="icon-menu"> <span class="glyphicon glyphicon-menu-hamburger"></span>MENU</div>

css CSS

.menu {
    position: fixed;
    width: 285px;
    height: 100%;
    left: -285px;
    background: #202024;
    z-index: 1;
}
.glyphicon-remove, ul {
    color: #fff;
    padding: 10px;
}
/* Added an invisible block */
.inv {
    display:none;
    width:100%;
    height:100%;
    position:fixed;
    margin:0;
}

jQuery jQuery的

"use strict";

var main = function () {
    $('.icon-menu').click(function () {
        $('.icon-menu').hide();
        $('.inv').show(); //added
        $('.menu').animate({
            left: "0px"
        }, 200);
    });
    //Added
    $('.inv').click(function () {
        $('.inv').hide();
        $('.menu').animate({
            left: "-285px"
        }, 200);
        $('.icon-menu').show();

    });


    $('.closed').click(function () {
        $('.inv').hide();//added
        $('.menu').animate({
            left: "-285px"
        }, 200);
        $('.icon-menu').show();

    });
};

$(document).ready(main);

Another simple example : http://jsfiddle.net/3v5ym2bp/17/ 另一个简单的例子: http//jsfiddle.net/3v5ym2bp/17/

I suggest you bind an event on document click after menu has been shown, the event that will ensure that every click anywhere outside menu will close it. 我建议您在菜单显示后绑定文档点击事件,该事件将确保菜单外的任何位置都会关闭它。

$(document).on("click.menu",function(event) {
            var target = $(event.target);   
            if (!target.closest(".menu").length || target.closest(".closed").length) {
                closeMenu(function() {
                    $(document).off("click.menu");
                });
            }           
        }); 

here you'll have to evaluate event object, and what trigger it - which element if it's inside menu (apart from the close button) then don't do nuffing, if outside - then close the menu. 在这里你必须评估事件对象,以及触发它的内容 - 如果它在菜单内部(除了关闭按钮)之外的哪个元素,那么如果在外面则不要进行骚乱 - 然后关闭菜单。 All the closing stuff is put to the separate function. 所有结束的东西都被放到了单独的函数中。 also don't forget to unbind this handler from document after closing 也不要忘记在关闭后从文档解除绑定

http://jsfiddle.net/3v5ym2bp/15/ http://jsfiddle.net/3v5ym2bp/15/

working demo 工作演示

Use css attribute left to detect if the menu is visible instead of :visibe because it's bot work with chrome, see jquery .is(“:visible”) not working in Chrome . 使用left css属性来检测菜单是否可见而不是:visibe因为它的机器人使用chrome,请参阅jquery .is(“:visible”)无法在Chrome中运行

You have just to detect if the menu is visible (use the css attribute left) because if the menu css left=0px that mean it's visible, and after that if the click is outside of menu or not and so if outside close it. 您只需要检测菜单是否可见(使用左侧的css属性),因为如果菜单css left=0px表示它是可见的,之后如果click是在菜单之外,那么如果外面关闭它。

Take a look at Your updating fiddle work fine just by adding the following handle that detect outside click : 只需添加以下检测外部点击的handle ,即可了解您的更新小提琴工作:

JS : JS:

$(document).click(function(e) {
    //if the menu is visible
    if($(".menu").css('left') =="0px"){
        //if the click is outside of menu
        if($(e.target).closest('.menu').length == 0){
            $('.closed').click();
        } 
    }       
});

 var main = function() { $('.icon-menu').click(function() { $('.icon-menu').hide(); $('.menu').animate({ left: "0px"}, 200); }); $('.closed').click(function() { $('.menu').animate({ left: "-285px"}, 200); $('.icon-menu').show(); }); }; $(document).ready(main); var rahul=0; $(window).click(function(e) { $('.menu').animate({ left: "-285px"}, 200); $('.icon-menu').show(); }); 
 .menu { position: fixed; width: 285px; height: 100%; left: -285px; background: #808080; z-index: 1; } .glyphicon-remove, ul { color: #fff; padding: 10px; } 
 <div class="menu" onclick="event.cancelBubble=true;"> <span class="glyphicon glyphicon-remove closed pull-right"></span> <ul> <li>menu1</li> <li>menu2</li> <li>menu3</li> </ul> </div> <div class="icon-menu" onclick="event.cancelBubble=true;"> <span class="glyphicon glyphicon-menu-hamburger"></span>*MENUS* </div> <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script> 

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

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