简体   繁体   English

Javascript IF屏幕宽度然后隐藏div

[英]Javascript IF screen width then hide div

Trying to get a result whereby if the window size is less that 900px then the items hide themselves on document load. 试图获得一个结果,如果窗口大小小于900px,则项目在文档加载时会隐藏起来。 However running into trouble as the code I know works when not in an IF wont work when it is enclosed in an IF statement? 但是,当我知道的代码不在IF中工作时会遇到麻烦,而将其包含在IF语句中会不会起作用?

 var searchEl = document.querySelector("#input"); if ($(window).width() > 960) { $(document).ready(function(){ $("#hide").load(function(){ $(".mobile-nav-hide").hide(); }); $("#toggle").click(function(){ $(".mobile-nav-hide").toggle(); }); }); } 
 h4.mobile-nav { font-family: GillSansMTStd-Medium; color: #5F5A51; display: block; text-align: center; padding: 15px 0px; text-decoration: none; } h4.mobile-nav:hover { background-color: #7BAF8A; } @media (min-width: 900px) { h4.mobile-nav { display: none; } } @media (min-width: 1024px) { h4.mobile-nav { display: none; } } nav { margin: 0 auto; padding: 0px; width: 0 auto; } @media (min-width: 900px) { nav { margin: 0 auto; padding: 0px; width: 851px; } } @media (min-width: 1024px) { nav { margin: 0 auto; padding: 0px; width: 924px; } } ul.nav { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #E8DDBF /*#E8DFCE*/; } @media (min-width: 900px) { ul.nav { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #E8DDBF /*#E8DFCE*/; height: 35px; } } @media (min-width: 1024px) { ul.nav { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #E8DDBF /*#E8DFCE*/; height: 40px; } } li.nav { width: auto; } @media (min-width: 900px) { li.nav { width: auto; float: left; margin-left: 15px; } } @media (min-width: 1024px) { li.nav { width: auto; float: left; margin-left: 15px; } } li.nav a.nav { font-family: GillSansMTStd-Medium; font-size: 14px; color: #5F5A51; display: block; text-align: center; padding: 12px 16px; text-decoration: none; } @media (min-width: 900px) { li.nav a.nav { font-family: GillSansMTStd-Medium; font-size: 12px; color: #5F5A51; display: block; text-align: center; padding: 12px 16px; text-decoration: none; } } @media (min-width: 1024px) { li.nav a.nav { font-family: GillSansMTStd-Medium; font-size: 14px; color: #5F5A51; display: block; text-align: center; padding: 14px 16px; text-decoration: none; } } li.nav a.nav:hover:not(.active) { background-color: #7BAF8A; color: #FFFFFF; } 
 <div id="nav-container"> <nav> <h4 class="mobile-nav" button id="toggle">Menu</h4> <div class="mobile-nav-hide"> <ul class="nav" class="search"> <li class="nav" style="margin-left: 0px;"><a class="nav" href="#home">TEXT &amp; TEXT</a></li> <li class="nav"><a class="nav" href="#home">TEXT</a></li> <li class="nav"><a class="nav" href="#home">TEXT</a></li> <li class="nav"><a class="nav" href="#home">TEXT</a></li> <li class="nav"><a class="nav" href="#home">TEXT</a></li> <li class="nav"><a class="nav" href="#home">TEXT &amp; TEXT</a></li> <li class="nav"><a class="nav" href="#home">TEXT &amp; TEXT</a></li> <li class="nav"><a class="nav" href="#home">TEXT</a></li> <li class="nav"><a class="nav" href="#home">TEXT</a></li> </ul> </nav> </div> 

UPDATE: I don't seem to have made it clear that the page needs to hide the li elements under the Menu item, for use in the hide/show, when the width < 900px and then have no effect then the width is over 900px. 更新:我似乎还没有弄清楚页面需要隐藏Menu项下的li元素,以便在隐藏/显示时使用,当宽度<900px时无效,然后宽度超过900px 。

In your case, you should prefer using only media queries for this problem. 对于您的情况,您应该更喜欢仅使用媒体查询来解决此问题。 There is no need for using javascript here. 此处无需使用javascript。 Besides that, your logic is flawed. 除此之外,您的逻辑是有缺陷的。

The reason behind this is that: 其背后的原因是:

  1. Your will not need to wait for the DOM ready callback to make it work, it will feel faster 您无需等待DOM就绪回调就可以正常工作,它将感觉更快
  2. Your logic fails consider resizing events (If the user starts at 970px width but then resizes to 700px width, what happens?) 您的逻辑失败,请考虑调整事件大小(如果用户以970px宽度开始,然后再调整为700px宽度,会发生什么情况?)
  3. There is no reason to use logic blocks for something that is merely visual and doable by CSS 没有理由将逻辑块用于仅由CSS可视且可行的东西

"But I really want to use javascript!" “但是我真的很想使用JavaScript!”

Take a look at this approach (not tested). 看一下这种方法(未经测试)。 It fixes your non-resizing-checking issue and adds a clean toggling of classes for your desired selector. 它解决了您的非调整大小检查问题,并为所需的选择器添加了干净的类切换。

$(document).ready(function() {

   function checkNeedForHiding() {
      // TODO Use throttling here
      $(".mobile-nav-hide").toggleClass("hidden", function() { return $(window).width() > 960; } ); 
   }

   $( window ).resize(function() {
     checkNeedForHiding();
   });

   checkNeedForHiding();
});

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

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