简体   繁体   English

JavaScript切换显示/隐藏响应菜单

[英]JavaScript Toggle Show/Hide Responsive Menu

I've been stuck on this particular problem. 我一直坚持这个特殊的问题。 I would like to use a toggle menu for mobile widths on a website that I'm working on. 我想在我正在处理的网站上使用移动宽度的切换菜单。 Basically, it works nicely as I can show you on this CodePen . 基本上,它可以很好地工作,因为我可以告诉你这个CodePen

The code for showing/hiding the menu via a toggle button with JavaScript works below. 使用JavaScript通过切换按钮显示/隐藏菜单的代码如下所示。

$(document).ready(function() {

  //Menu Open Seasame Action    
  $('.site-nav-btn').click(function() {
  $('.site-nav ul').slideToggle('fast');
  $(this).find('span:hidden').show().siblings().hide();
});

  //Hide site-nav content.    
  $(".site-nav ul").hide();
});

My problem is that I would like to hide the toggle button and the toggle function when the width exceeds say, 480 pixels wide but keep the site-nav ul visible. 我的问题是我想隐藏切换按钮和切换功能,当宽度超过说,480像素宽但保持site-nav ul可见。 I've been trying to do this via this code combined with the one above, and somehow it just doesn't work. 我一直试图通过这个代码结合上面的代码来做到这一点,并且不知何故它只是不起作用。

$(function() {
  if ($(window).width() > 480) {
      $('.site-nav-btn').css('display','none');
      $('.site-nav ul').show();
      }
  else {
      $('.site-nav-btn').css('display','block');
      $('.site-nav ul').hide();
      }
  });

I'm not really that proficient in JavaScript so if anyone could point out why it didn't work alongside the solutions that would really be helpful. 我不是那么精通JavaScript,所以如果有人能指出为什么它不能与那些真正有用的解决方案一起工作。

Attach your checking function to $(window).resize() and fix the selectors. 将检查功能附加到$(window).resize()并修复选择器。 See this fork of your CodePen . 请参阅CodePen的此分支

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

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