简体   繁体   English

jQuery隐藏和显示糟糕的CSS媒体查询

[英]jQuery hide and show trumping CSS media queries

I have this button that appears when the window is smaller then 500px. 当窗口小于500px时,我会显示此按钮。 This button shows a div. 此按钮显示div。

I also have a resize function that makes the div dissapear if the window gets larger then 500px. 我还具有一个调整大小的功能,如果窗口变大然后500px,div就会消失。

The only problem is, I don't want #nav_open to ever be shown if the width is greater then 500px but my jquery is overwriting my @media requests. 唯一的问题是,如果宽度大于500px,我不希望显示#nav_open ,但是我的jQuery正在覆盖我的@media请求。

Can anyone show me what I may be doing wrong and have the CSS trump the jquery in some facet? 谁能告诉我我可能做错了什么,让CSS在某些方面胜过jquery?

JSFIDDLE JSFIDDLE

HTML 的HTML

<div id="nav_close">OPEN</div>
<div id="nav_open">CLOSE</div>
<div id="mobile-nav-container">
  <div class="mobile-menu">
    <ul class="menu">
      <li><a href="/">Home</a></li>
      <li><a href="/resellers.html">FOR RESELLERS</a></li>
      <li><a href="/merchants.html">MERCHANTS</a></li>
      <li><a href="/equipment.html">EQUIPMENT</a></li>
      <li><a href="/about.html">ABOUT</a></li>
      <li><a id="contact_button" href="#">CONTACT</a></li>
    </ul>
  </div>
</div>

CSS 的CSS

#nav_close, #nav_open, #mobile-nav-container { display:none; }
@media (max-width:500px) { #nav_close { display:block;} }

JS JS

$(document).ready(function() {

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

  function resize() {
    if ($(window).width() > 500 && $('#mobile-nav-container').is(':visible')) {
      $('#mobile-nav-container').hide();
      $("#nav_open").hide();
      $("#nav_close").show();
    }
  }

  $('#nav_open, #nav_close').click(function() {
    $('#mobile-nav-container').toggle();
    $("#nav_open").toggle();
    $("#nav_close").toggle();
  });

});

Jquery's toggle() , show() , hide() methods apply the styles inline. jQuery的toggle()show()hide()方法可内联应用样式。 Per CSS rules of specificity , inline styles will override even your media queries. 根据CSS的特定性规则,内联样式甚至会覆盖您的媒体查询。 Although this should be generally avoided, you can add the !important keyword in your media queries so that it overrides the inline styles. 尽管通常应该避免这种情况,但是您可以在媒体查询中添加!important关键字,以使其覆盖内联样式。

CSS: CSS:

@media (max-width:500px) { 
    #nav_close { 
        display: block !important;
    }
}

When you do .hide() it is roughly equivalent to doing 当您执行.hide()时,它大致等效于

.css( "display", "none" )

This will apply inline class to your element and inline classes have precedence over css classes :) Now, if you still want your css selectors to take precedence over jQuery you can use !important in your css classes which may help you achieving your use case. 这会将内联类应用于您的元素,并且内联类优先于css类:)现在,如果您仍然希望css选择器优先于jQuery,则可以在css类中使用!important ,这可以帮助您实现用例。 But you may want to consider your design first. 但是您可能需要首先考虑您的设计。

Hope this be of some help. 希望这会有所帮助。

Happy Learning :) 快乐学习:)

Using toggleClass('hide') (or addClass('hide') and removeClass('hide') ) instead of just show() or hide() will work for you as long as the bootstrap css file is loading before (above) your custom css file. 只要不使用show()hide()就可以使用toggleClass('hide') (或addClass('hide')removeClass('hide') )代替,只要在启动Bootstrap CSS文件之前(以上)您的自定义CSS文件。 Your custom css will override bootstrap's hide class. 您的自定义css将覆盖bootstrap的hide类。

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

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