简体   繁体   English

跨浏览器同步CSS和JS媒体查询

[英]Syncing CSS and JS media queries cross-browser

When working on responsive designs I make use of CSS3 media queries, including with older browsers using respond.js. 在处理响应式设计时,我使用CSS3媒体查询,包括使用respond.js的旧浏览器。 There are occasions when I also need to use JavaScript/jQuery for manipulating some elements on the page. 有时候我还需要使用JavaScript / jQuery来操作页面上的一些元素。 However, when using $(window).width(), that value is often different than the value used in the CSS media query due to browser chrome and/or the presence of a vertical scrollbar, meaning a CSS media query will fire at a different breakpoint than the JS media query. 但是,当使用$(window).width()时,由于浏览器镶边和/或存在垂直滚动条,该值通常不同于CSS媒体查询中使用的值,这意味着CSS媒体查询将在不同的断点比JS媒体查询。

I know Modernizr alleviates this issue through the use of Modernizr.mq, but that only works in browsers that support CSS media queries to begin with, generally meaning it won't work in IE8 and below. 我知道Modernizr通过使用Modernizr.mq缓解了这个问题,但这只适用于支持CSS媒体查询的浏览器,通常意味着它在IE8及以下版本中不起作用。 And if you use a polyfill for adding media query support to these older browsers (eg, respond.js), then the Modernizr.mq no longer works due to a conflict/override between the two. 如果您使用polyfill为这些旧版浏览器添加媒体查询支持(例如,respond.js),那么Modernizr.mq将因两者之间的冲突/覆盖而不再有效。

What I've instead done is made use of a hidden input element on the page and given it a CSS width style within each of the media queries. 我所做的是在页面上使用隐藏的输入元素,并在每个媒体查询中为其指定CSS宽度样式。 I then have a function that runs automatically on the page (and on resize) that gets that width value and executes the following: 然后我有一个在页面上自动运行的函数(以及调整大小),它获取该宽度值并执行以下操作:

        if (width == "320px" ) {
            //Functions
        }

        //481px to 600px
        else if (width == "481px" ) {
            //Functions
        }

        //601px to 768px
        else if (width == "601px" ) {
            //Functions
        }

        //769px to 960px
        else if (width == "769px" ) {
            //Functions
        }

        //769 to 1024px
        else if (width == "961px" ) {
            //Functions
        }

        //1024px+
        else {
            //Functions
        }

This basically forces the JS to work entirely off of the CSS media query, syncing the two. 这基本上迫使JS完全脱离CSS媒体查询,同步两者。 This can become more specific rather than generic as I have it, instead firing based off of a specific element and a specific style that changes on that element, it depends on individual project needs. 这可以变得更具体,而不是像我一样,而是基于特定元素和特定样式进行触发,这取决于单个项目的需求。

My question then is, is there a simpler way of doing this? 那么我的问题是,有没有更简单的方法呢? I've spent considerable time looking into and experimenting with this and it seems the best option thus far that takes into account older browsers as well. 我花了相当多的时间研究和试验这个,它似乎是迄今为止考虑到旧浏览器的最佳选择。

PPK listed a nice way to pair the CSS & JS on his blog which sort of backs up your method: PPK列出了一种在他的博客上配对CSS和JS的好方法,它可以支持你的方法:

@media all and (max-width: 900px) {
// styles
}

if (document.documentElement.clientWidth < 900) {
   // scripts
}

His full post on it is here . 他的完整帖子就在这里

There's also the enquire.js plug-in. 还有enquire.js插件。

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

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