简体   繁体   English

响应菜单在窗口调整大小时消失

[英]Responsive menu disappearing on window resize

I'm a bit of a novice with script but have been trying to find a solution that fits my responsive menu solution. 我对脚本有点新手,但是一直在尝试找到适合我的响应菜单解决方案的解决方案。 I've seen other people with a similar issue but they seem to be using a different method for their menus. 我见过其他人也遇到类似的问题,但他们的菜单似乎使用了不同的方法。

Here is my code: 这是我的代码:

HTML: HTML:

 function toggle_visibility(id) { var e = document.getElementById('menu-items'); if ($(e).css('display') == 'block') { $(e).slideUp('fast'); } else { $(e).slideDown('fast'); } }; 
 .mobile-menu { display: none } @media only screen and (max-width: 680px) { #menu-items { display: none } .mobile-menu { display: block; cursor: pointer; } } 
 <a onclick="toggle_visibility('menu-items');" class="mobile-menu">Menu</a> <div id="menu-items"> <a href="#">Link</a> <a href="#">Link</a> <a href="#">Link</a> <a href="#">Link</a> <a href="#">Link</a> </div> <!--#menu-items--> 

The menu works great but the main issue I am having is that when the user decreases the window size to show the responsive menu toggle, then uses the toggle to open and then close the menu, the menu never returns to the normal view when the window is resized back to desktop view. 菜单效果很好,但是我遇到的主要问题是,当用户减小窗口大小以显示响应菜单切换时,然后使用该切换打开并关闭菜单时,菜单永远不会返回到正常视图。调整大小回到桌面视图。

Oddly it does return if the user leaves the responsive menu open in mobile view and resizes back to desktop, but not if the user closes the menu. 奇怪的是,如果用户在移动视图中将响应菜单保持打开状态并重新调整为桌面大小,它将返回,但是如果用户关闭菜单,则不会返回。

The only other thing I'd love to work on this menu is if the user clicks anywhere else in the page but the menu, the menu closes. 我唯一想在此菜单上使用的其他东西是,如果用户单击页面中除菜单之外的其他任何位置,菜单将关闭。 At the moment the user has to click the Menu toggle link to close it. 目前,用户必须单击“菜单”切换链接将其关闭。

Any help would be really appreciated!!! 任何帮助将非常感激!!!

Thanks so much. 非常感谢。

Try this : 尝试这个 :

@media only screen and (min-width: 680px) {


 #menu-items {
    display: block
  }
  .mobile-menu {
    display: none;
    cursor: pointer;
  }

}

Using slideUp() and slideDown() here is a little tricky because they set/remove the inline style 'display: none;' 在这里使用slideUp()和slideDown()有点棘手,因为它们设置/删除了内联样式'display:none;'。 which isn't removed when you resize the window. 调整窗口大小时不会将其删除。 That's why the menu isn't reappearing: the inline style hiding the menu is still active. 这就是菜单没有出现的原因:隐藏菜单的嵌入式样式仍然处于活动状态。

What you need to do is use classes to do handle the display at different device widths and add hooks to slideUp's complete callback: ( JSFiddle ) 您需要做的是使用类来处理不同设备宽度的显示,并向slideUp的完整回调添加钩子:( JSFiddle

CSS: CSS:

.mobile-menu {
    display: none
}

@media only screen and (max-width: 680px) {
    #menu-items {
        display: none
    }
    .mobile-menu {
        display: block;
        cursor: pointer;
    }
    .collapsed {
        display: none;
    }
}

@media only screen and (min-width: 681px) {
    .collapsed {
        display:block;
    }
}

JS: JS:

function toggle_collapsed_class(e) {
    $(e).css('display', '').addClass('collapsed');
};

function toggle_visibility(id) {
    var e = document.getElementById('menu-items');
    if ($(e).css('display') == 'block') {
        $(e).slideUp('fast', function(){
            toggle_collapsed_class(e)
        });
    } else {
        $(e).removeClass('collapsed').slideDown('fast');
    }
};

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

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