Make Slicknav menu close when outside click on IOS devices

梦想与她 提交于 2019-12-11 08:04:37

问题


I'm using the Slicknav mobile menu script: http://slicknav.com/

And it's working great except I can't figure out how to get it to close when one clicks outside of the menu on ios devices. It closes fine when clicking outside when I test it on desktop and Android, but on my Ipad it's not working. Here's the current code. Any insight? Thank you!

<script src="slicknav.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.menu').slicknav({
label:'',
closeOnClick:true
});
$('.slicknav_menu').focusout(function(event){
$('.menu').slicknav('close');
});
});
</script>


<nav>
<ul class="menu">
<li><a href="">Home</a></li>
<li><a href="">Link1</a></li>
<li><a href="">Link2</a></li>
</ul>
</nav>

回答1:


On my site the right menu uses slicknav. It closes fine when i click outside on iOS thanks to this code. Demonstration : http://www.crealisationweb.fr

Replace this:

$('.slicknav_menu').focusout(function(event){
    $('.menu').slicknav('close');
});

With this :

$("div, html").on("click", function (event) { 
    if(!$(event.target).hasClass(".menu a") && 
    !$(event.target).hasClass("ul.slicknav_nav li a") && 
    !$(event.target).hasClass("slicknav_menutxt") && 
    !$(event.target).hasClass("slicknav_icon") && 
    !$(event.target).hasClass("slicknav_icon-bar") &&
    !$(event.target).hasClass("slicknav_btn")) {   
        $(".menu").slicknav('close'); 
    }
});



回答2:


I used something like this

$(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');
}
});



回答3:


I remember creating my first mobile website and this issue killed so much time. iOS has a known issue with :hover state when clicked.

Read up here - it should fix your issue.



来源:https://stackoverflow.com/questions/29658550/make-slicknav-menu-close-when-outside-click-on-ios-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!