简体   繁体   English

CSS和jQuery有不同的宽度

[英]CSS and jQuery have different widths

I'm, setting up the mobile side of a website at the moment, and I need custom CSS and Javascript for mobile, so in the CSS I have rules using @media screen and (max-width: 500px) { and in Javascript I was going to use if ($(window).width() < 500 . 我现在正在设置一个网站的移动端,我需要自定义CSS和移动设备的Javascript,所以在CSS我有规则使用@media screen and (max-width: 500px) {和在Javascript我将使用if ($(window).width() < 500

However, if I resize my browser to the exact pixel the mobile CSS starts being used and I console.log($(window).width()); 但是,如果我将浏览器的大小调整为精确的像素,则开始使用移动CSS,并且我使用console.log($(window).width()); I get 485. 我得到了485。

Is this normal behaviour or am I doing something wrong? 这是正常行为还是我做错了什么?

Update: 更新:

Using this, the values seem to be in sync, only tested in firefox though at the moment. 使用这个,值似乎是同步的,虽然目前只在Firefox中测试过。

var scrollBarWidth = false;


function mobileRules() {

    if (!scrollBarWidth) {
        var widthWithScrollBars = $(window).width();
        $('body').css('overflow', 'hidden');
        var widthNoScrollBars = $(window).width();
        $('body').css('overflow', 'scroll');
        scrollBarWidth = widthNoScrollBars - widthWithScrollBars;
        console.log('Width: '+widthWithScrollBars+'. Without: '+widthNoScrollBars+'. Scroll: '+scrollBarWidth);
    }

    console.log($(window).width()+scrollBarWidth+' vs '+globals.mobile_width);

    if ($(window).width()+scrollBarWidth < globals.mobile_width) {
        console.log('Running mobile rules in jQuery');
    }
}

In firefox, media queries consider the width of the scrollbar to be inside the screen width. 在firefox中,媒体查询会将滚动条的宽度视为屏幕宽度。

This is what gives you the 15px wider screen width. 这就是为您提供15px更宽的屏幕宽度。

In webkit based browsers they don't. 在基于webkit的浏览器中,他们没有。

If you're interested in why this thing happens, I'll quote this comment of this article : 如果你对这件事发生的原因感兴趣,我会引用这篇文章的评论

A problem with Webkit browsers (that aren't following spec) is that the browser can encounter an infinite loop condition caused by media queries, which leads to a browser crash. Webkit浏览器(不遵循规范)的一个问题是浏览器可能遇到由媒体查询引起的无限循环条件,从而导致浏览器崩溃。

For example: >500px overflow-y: scroll, <500px overflow-y: hidden. 例如:> 500px overflow-y:scroll,<500px overflow-y:hidden。 Size your browser to 505px window width. 将浏览器的大小调整为505px窗口宽度。 Since the scroll bar subtracts 15 or so pixels from the width used by the media query, the media query flips you to < 500, but as soon as you hit <500 the scrollbar goes away, and the media query flips you to >500, and then the fun starts because now you have a scroll bar again and you're <500px and you get that style with no scroll bar... Rinse and repeat until the browser finally dies. 由于滚动条从媒体查询使用的宽度中减去15个左右的像素,因此媒体查询会将您翻转为<500,但只要您点击<500,滚动条就会消失,媒体查询会将您翻转到> 500,然后有趣的开始,因为现在你再次有一个滚动条,你是<500px,你得到的风格没有滚动条...冲洗并重复,直到浏览器最终死亡。

Now, write some javascript to calculate the media query max widths, and you have a page that will crash Chrome/Safari as soon as you load it. 现在,编写一些javascript来计算媒体查询的最大宽度,并且您有一个页面会在加载Chrome / Safari后立即崩溃。

My guess is that the spec was written the way it was to prevent this condition. 我的猜测是,规范是按照防止这种情况的方式编写的。 Firefox & Opera are following spec, it's not really their fault you don't agree with spec. Firefox和Opera遵循规范,你不同意规范并不是他们的错。

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

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