简体   繁体   English

响应式移动菜单的jQuery问题

[英]jQuery issue with responsive mobile menu

I am using this Responsive Mobile Menu for my site now I want to show/hide only when user click to bellow image area. 我现在在我的网站上使用此“ 响应式移动菜单” ,我只想在用户单击下面的图像区域时显示/隐藏。 Currently menu will hide if user click to any link and that I don't want. 如果用户单击任何链接并且我不想要,当前菜单将隐藏。

在此处输入图片说明

My JS Code: 我的JS代码:

function responsiveMobileMenu() {   
    $('.rmm').each(function() {
        $(this).children('ul').addClass('rmm-main-list');   // mark main menu list
        var $style = $(this).attr('data-menu-style');   // get menu style
        if ( typeof $style == 'undefined' ||  $style == false )
        {
            $(this).addClass('graphite'); // set graphite style if style is not defined
        }
        else {
            $(this).addClass($style);
        }
        /*  width of menu list (non-toggled) */
        var $width = 0;
        $(this).find('ul li').each(function() {
            $width += $(this).outerWidth();
        });
        // if modern browser
        if ($.support.leadingWhitespace) {
            $(this).css('max-width' , 1934+'px');
        }
        // 
        else {
            $(this).css('width' , 1934+'px');
        }
    });
}
function getMobileMenu() {
    /*  build toggled dropdown menu list */
    $('.rmm').each(function() { 
        var menutitle = $(this).attr("data-menu-title");
        if ( menutitle == "" ) {
            menutitle = "Menu";
        }
        else if ( menutitle == undefined ) {
            menutitle = "Menu";
        }
        var $menulist = $(this).children('.rmm-main-list').html();
        var $menucontrols ="<div class='rmm-toggled-controls'><div class='rmm-toggled-title'>" + menutitle + "</div><div class='rmm-button'><span>&nbsp;</span><span>&nbsp;</span><span>&nbsp;</span></div></div>";
        $(this).prepend("<div class='rmm-toggled rmm-closed'>"+$menucontrols+"<ul>"+$menulist+"</ul></div>");
    });
}

function adaptMenu() {

    /*  toggle menu on resize */

    $('.rmm').each(function() {
        var $width = $(this).css('max-width');
        $width = $width.replace('px', ''); 
        if ( $(this).parent().width() < $width*1.05 ) {
            $(this).children('.rmm-main-list').hide(0);
            $(this).children('.rmm-toggled').show(0);
        }
        else {
            $(this).children('.rmm-main-list').show(0);
            $(this).children('.rmm-toggled').hide(0);
        }
    });

}

$(function() {

    responsiveMobileMenu();
    getMobileMenu();
    adaptMenu();

    /* slide down mobile menu on click */

    $('.rmm-toggled, .rmm-toggled .rmm-button').click(function(){
        if ( $(this).is(".rmm-closed")) {
            $(this).find('ul').stop().show(300);
            $(this).removeClass("rmm-closed");
        }
        else {
            $(this).find('ul').stop().hide(300);
            $(this).addClass("rmm-closed");
        }
    }); 

});
/*  hide mobile menu on resize */
$(window).resize(function() {
    adaptMenu();
});

My JSFiddle: Sample 我的JSFiddle: 样本

Any idea or suggestion? 有什么想法或建议吗?

Menu is toggled because events in DOM are propagated in DOM tree. 切换菜单是因为DOM中的事件在DOM树中传播。 You have to call event.stopPropagation() function, to prevent this behaviour. 您必须调用event.stopPropagation()函数,以防止此行为。

You have to add simple line of code: 您必须添加简单的代码行:

$('.rmm li').click( function( e) {
   e.stopPropagation();
});

Here is updated fiddle: http://jsfiddle.net/PPA6j/1/ 这是更新的小提琴: http : //jsfiddle.net/PPA6j/1/

Im also suggesting add some class to your li elements, for example do-not-hide , then it should looks like: 我还建议向您的li元素添加一些类,例如do-not-hide ,那么它应该类似于:

$('.rmm li.do-not-hide').click( function( e) {
    e.stopPropagation();
});

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

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