简体   繁体   English

Slicknav:如何在外部单击时关闭菜单

[英]Slicknav: how to close menu when clicking outside

I am using the SlickNav jQuery plugin , but it seems that auto-closing the menu on blur or lost focus is not set by default and there is no setting for it. 我正在使用SlickNav jQuery插件 ,但是默认情况下似乎没有设置模糊或失去焦点时自动关闭菜单,也没有设置。

I thought it was just a simple call like this: 我以为这只是一个简单的电话:

 <script>
  $(document).ready(function() {
    //close menu on lost focus
    $('.js .slicknav_menu').focusout(function(event){
     $(this).slicknav('close');
    });  
  });
 </script>

But this doesn't do anything. 但这没有任何作用。 How do I close the menu when clicking outside of the menu? 在菜单外单击时如何关闭菜单?

I referred the site that you mentioned in your question. 我提到了您在问题中提到的网站。 Tried the first example given in the site. 尝试了站点中给出的第一个示例。 (HTML code of which is given below) (以下给出了HTML代码)

HTML - HTML-

<ul id="menu">
    <li><a class="scroll" href="#features">Features</a></li>
    <li><a class="scroll" href="#usage">Usage Instructions</a></li>
    <li><a class="scroll" href="#examples">Examples</a></li>
    <li><a href="http://github.com">View on Github</a></li>
</ul>

I saw menu was not closing by default on lost focus event. 我看到菜单在默认情况下不会因丢失焦点事件而关闭。 Below code worked to achieve the same. 下面的代码可以实现相同的目的。

$(document).ready(function() {
    //close menu on lost focus
    $('.slicknav_menu').focusout(function(event){
        $('#menu').slicknav('close'); //Here 'menu' is the id of ul.
    });  
  });

I used something like this. 我用过这样的东西。
This works also on mobiles. 这也适用于手机。

$(window).on("touchstart", function(e) {
    var container = $("#menu");
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
{
    $('#menu').slicknav('close');
}
});

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

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