简体   繁体   English

CSS媒体屏幕宽度与jQuery(window).width()

[英]CSS media screen width vs jQuery(window).width()

I am making some nifty CSS3 animations assisted by some jquery and in the process of making those animated functions responsive, I stumbled upon a strange thing, very strange indeed. 我正在制作一些漂亮的CSS3动画,并辅以一些jquery,并在使这些动画功能具有响应性的过程中,偶然发现了一件奇怪的事情,的确非常奇怪。

The media query I am calling states 我正在呼叫的媒体查询状态

@media screen and (max-width: 1024px)

But when I call the window width using JavaScript it reveals that it actually triggers at window width 1009px 但是,当我使用JavaScript调用窗口宽度时,它表明它实际上是在窗口宽度1009px处触发的

console.log('window.size: '+$(document).width());

I must admit that I am completely confused by this one, anyone have any bright idea? 我必须承认,我对此完全感到困惑,有人有什么好主意吗? :) :)

After @Pete pointed me in the right direction and ispired by a small pice of code I found somewhere, I came up with this: @Pete向我指出正确的方向并被我在某处发现的一小段代码所激怒之后,我想到了:

(I would give credits to the person who came up with the below if I could remember where I found it, but posting it here in case someone else needs a similar solution) (如果我能记住我在哪里找到的,我会给提出以下内容的人以功劳,但如果有人需要类似的解决方案,请在此处发布)

function scrollBarWidth() {
    jQuery("html").css("overflow", 'hidden');
    var width = jQuery("html").width();
    jQuery("html").css("overflow", 'scroll');
    width -= jQuery("html").width(); 
    if(!width){
        width = document.body.offsetWidth - document.body.clientWidth;
        jQuery("body").css("overflow", '');
    }
    return width; 
}

I have had the same issue, I believe this is something to do with jQuery, the solution I have found that may be a little more light weight that a whole function, is to use 我遇到过同样的问题,我相信这与jQuery有关,我发现该解决方案可能比整个函数要轻一些。

window.innerWidth 

instead of using jQuery to select the body/window width. 而不是使用jQuery选择主体/窗口宽度。

Here is a fiddle of it working without the jQuery selector http://jsfiddle.net/wf40d79x/ 这是在没有jQuery选择器的情况下工作的小提琴http://jsfiddle.net/wf40d79x/

using 运用

$(window).resize(function() {
    console.log(window.innerWidth);
});

and here is it breaking, WITH the jQuery selector http://jsfiddle.net/4bgzf1Lp/2/ 使用jQuery选择器http://jsfiddle.net/4bgzf1Lp/2/

using 运用

$(window).resize(function() {
    console.log($(window).width());
});

You'll see in the console as you bring the screen down to 600px the as the media query pops, using javascript only the console will agree with the width, whereas with jQuery it will be about 17px smaller. 当媒体查询弹出时,您会在控制台中看到将屏幕降低到600像素的情况,使用javascript时,只有控制台会同意宽度,而使用jQuery时,它将缩小约17像素。

Hope this helps. 希望这可以帮助。

I came across a solution which is brilliant to verify which media file is active. 我遇到了一个很好的解决方案,可以验证哪个媒体文件处于活动状态。 Add a selector which changes state or font in css media file and check in js to check if state has changed. 在CSS媒体文件中添加一个更改状态或字体的选择器,并在js中签入以检查状态是否已更改。

.width-verify{
    display: none;
}

@media only screen and (min-width: 768px) {
    .width-verify {
        display: block;
    }
}

JS: JS:

if ($('.width-verify').css('display') === 'block') {
    //...
}

Thanks to http://www.acoupleofnerds.com.au/2014/09/3-ways-fix-jquerys-window-width-method-matching-media-queries/ 感谢http://www.acoupleofnerds.com.au/2014/09/3-ways-fix-jquerys-window-width-method-matching-media-queries/

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

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