简体   繁体   English

如何使用Enquire.js动态添加和删除CSS类

[英]How do I use Enquire.js to add and remove css classes dynamically

So I want to add and remove a css class dynamically based on a media query with Enquire.js but I need help with using the functions. 因此,我想基于带有Enquire.js的媒体查询来动态添加和删除css类,但是我需要使用这些功能的帮助。

Here is what I want in sort of pseudo form: 这是我想要的一种伪形式:

if (screens max-width > 480px){

#thumb.removeClass('col-xs-12').addClass('col-xs-6');

}

Here is the thumb element to start with: 这是以thumb开头的元素:

<div id ="thumb" class="col-xs-12 col-md-3">
    <a href="#" class="thumbnail">
      <img src="images/thumbnails/golden.jpg" class="img-rounded img-responsive" alt="...">
    </a>
  </div>

How do I do this with Enquire.js assuming I have included all the necessary files. 假设我已包含所有必要文件,如何使用Enquire.js做到这一点。

There's a great tutorial here on using enquire.js. 这里有一个很好的教程,介绍如何使用enquire.js。

Having said that, I'm guessing that your real issue is that you are frustrated ( like so many others are ) by the lack of another breakpoint in Bootstrap between 0 and 767px. 话虽如此,我猜测您的真正问题是,由于Bootstrap中没有另一个介于0到767px之间的断点,您感到沮丧( 就像其他许多人一样 )。

Can I share one of my super-secret tricks with you? 我可以和你分享我的一个超级秘密把戏吗? Instead of trying to hack things with enquire, just mark up your page so that the xs cols are how you want the grid to work between 481px and 768px. 而不是尝试用enquire破解,只需标记您的页面,使xs cols成为您希望网格在481px和768px之间工作的方式。 Then, just add this media query: 然后,只需添加以下媒体查询:

@media (max-width:480px){ /* technically you can set this to any value you prefer that is below 767 */
    [class^="col-xs-"]{
        float:none;
        width:auto;
    }   
} 

What this does is basically adds another breakpoint for the grid. 这基本上是为网格添加另一个断点。 Obviously, it won't change things like the responsive helper classes (visible/hidden), but you could add your own classes for those if you really need it. 显然,它不会像响应式助手类(可见/隐藏)那样进行更改,但是如果您确实需要它,可以为这些类添加自己的类。

So, for example, your markup would look like this: 因此,例如,您的标记如下所示:

<div class="col-xs-6 col-md-3">
    <a href="#" class="thumbnail">
      <img src="images/thumbnails/golden.jpg" class="img-rounded img-responsive" alt="...">
    </a>
</div>

With that media query in place, the col-xs-6 will behave like col-xs-12 below 481px. 使用该媒体查询后,col-xs-6的行为将类似于481px以下的col-xs-12。

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

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