简体   繁体   English

如何仅对特定媒体查询使用jquery .css()?

[英]How to only use jquery .css() on a specific media query?

When a user's browser has a width of less than 1160px, a media query is setup to collapse my site's right sidebar. 当用户的浏览器宽度小于1160像素时,会设置媒体查询以折叠我的网站的右侧边栏。 They can get it back by clicking on a little arrow and then it will overlap the content (absolute positioning). 他们可以通过点击一个小箭头将其取回,然后它将与内容重叠(绝对定位)。

Anyway, I used the following jQuery to achieve this: 无论如何,我使用以下jQuery来实现这一目标:

$('.right_sidebar_preview').on('click', function() {
     $('.right_sidebar_preview').css('display', 'none');
     $('.right_sidebar').css('display', 'block');
});
$('.right_sidebar').on('click', function() {
     $('.right_sidebar_preview').css('display', 'block');
     $('.right_sidebar').css('display', 'none');
});

So basically I already have the preview hidden when the browser is larger than 1160px and the sidebar is visible, in the media query I have it set up so when it's below 1160px, the sidebar becomes invisible and the "sidebar preview" is shown which users can click to make it bigger. 所以基本上当浏览器大于1160px并且边栏可见时我已经隐藏了预览,在我设置的媒体查询中,当它低于1160px时,侧边栏变得不可见,并且“侧边栏预览”显示哪些用户可以点击使其变大。

CSS: CSS:

.right_sidebar {
  width: 242px;
  height: 100%;
  background-color: #d8e1ef;
  float: right;
  position: relative;
}
.right_sidebar_preview {
  display: none;
}
@media(max-width:1160px) {
  .right_sidebar {
    position: absolute;
    right: 0;
    display: none;
  }
  .right_sidebar_preview {
    display: block;
    background-color: #d8e1ef;
    position: absolute;
    right: 0;
    width: 15px;
    height: 100%;
  }
}

My question is, when I use the code above, let's say we're in the less than 1160px media query, I'll open the sidebar then collapse it again, and when I stretch my browser out to go back, it also closed the sidebar on the greater than 1160px media query. 我的问题是,当我使用上面的代码时,假设我们处于不到1160px的媒体查询中,我会打开侧边栏然后再次折叠它,当我伸出浏览器返回时,它也关闭了侧栏上大于1160px的媒体查询。

So is there anyway I can use .css() (or an alternative method) to point at a specific media query? 那么无论如何我可以使用.css()(或替代方法)来指向特定的媒体查询?

Do not manipulate the CSS display: property. 不要操纵CSS display: property。 Instead, use CSS classes to control the visibility of sidebar and its handle. 相反,使用CSS类来控制侧边栏及其句柄的可见性。 This way you can use your media query to control whether an element displays or not. 这样,您可以使用媒体查询来控制元素是否显示。

In the following demo, click the "Full page" button to see how this works on screens > 1160px. 在下面的演示中,单击“整页”按钮以查看其在屏幕上的工作方式> 1160px。

 $(function() { $('.right_sidebar, .right_preview').on('click', function() { $('.right_sidebar, .right_preview').toggleClass('lt-1160-hide lt-1160-show'); }); }); 
 .right_sidebar { width: 100px; height: 100px; background-color: papayawhip; float: left; display: block; } .right_preview { width: 20px; height: 100px; background-color: palegoldenrod; float: left; display: none; } @media(max-width: 1160px) { /* note: the following rules do not apply on larger screens */ .right_sidebar.lt-1160-show, .right_preview.lt-1160-show { display: block; } .right_sidebar.lt-1160-hide, .right_preview.lt-1160-hide { display: none; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="right_sidebar lt-1160-hide">Sidebar</div> <div class="right_preview lt-1160-show">&rsaquo;</div> 

On a recent project, I had to process a single function in jQuery based on the browser size. 在最近的一个项目中,我不得不根据浏览器大小处理jQuery中的单个函数。 Initially I was going to check the device width was applicable to a mobile device (320px): 最初我要检查设备宽度是否适用于移动设备(320px):

jQuery jQuery的

$(window).resize(function(){

       if ($(window).width() <= 320) {  

              // your code here

       }     

});

or 要么

$(window).resize(function(){     

       if ($('header').width() == 320 ){

              // your code here

       }

});

Something like this? 像这样的东西?

$(window).resize(function(){
   if ($(window).width() <= 1160){  
      // lets party
   }    
});

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

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