简体   繁体   English

如何使用Javascript获取'css-pixel'(媒体查询)宽度?

[英]How do I get the 'css-pixel' (media-query) width using Javascript?

I realize similar questions have been asked before regarding getting device sizes etc. but there seems to be disagreement about the exact way to go about this. 我意识到在获得设备尺寸等方面之前已经提出了类似的问题,但是对于确切的方法似乎存在分歧。

Suppose I have media-queries (eg Twitter Bootstrap) with 假设我有媒体查询(例如Twitter Bootstrap)

Extra small devices Phones (<768px) 超小型设备手机(<768px)

Small devices Tablets (≥768px) 小型设备平板电脑(≥768px)

Medium devices Desktops (≥992px) 中型设备台式机(≥992px)

Large devices Desktops (≥1200px) 大型设备台式机(≥1200px)

Now, I'd like to be able to use Javascript to essentially get the same output. 现在,我希望能够使用Javascript基本上获得相同的输出。 In other words, the css media-query pixel number rather than the actual screen dots. 换句话说,css媒体查询像素数而不是实际的网点。 ie I don't need to get the exact answer, but I want to avoid being out by a factor or 2 or 3 because I'm getting 'screen dots' rather than css pixels. 即我不需要得到确切的答案,但我想避免出一个因素或2或3,因为我得到'屏幕点'而不是css像素。

I'm more concerned with mobile devices - especially Android - than desktop. 我更关心的是移动设备 - 尤其是Android - 而不是桌面设备。 Is screen.width reliable for this? screen.width对此可靠吗? Or jQuery(document).width() ? 还是jQuery(document).width()

(This is for a research project and while I'm aware that it's basically impossible to detect tablet vs. phone, I'd like an indication of the size of the devices people are using. The user experience will not be dictated by the results.) (这是针对一个研究项目,虽然我知道基本上不可能检测平板电脑与手机,但我想了解人们使用的设备的大小。用户体验不会受到结果的影响。)

My favourite way of doing this is as follows (jQuery): 我最喜欢这样做的方式如下(jQuery):

var viewportWidth = $(window).width();
// Then  create if statements and execute code accordingly... 
if (viewportWidth <= 767) {
           // execute code
} else if (viewportWidth >= 768) {
           // execute this
}

You can also execute code for viewports between widths, such as: 您还可以在宽度之间执行视口代码,例如:

if (viewportWidth >= 768 && viewportWidth <= 991) {
   //execute this
}

This would execute the code when the viewport is between 768px and 991px... 这将在视口介于768px和991px之间时执行代码...

You can get window width using Javascript like this 您可以使用这样的Javascript获取窗口宽度

var wid = window.innerWidth;

Extra small devices Phones (<768px) 超小型设备手机(<768px)

if (wid < 767) {
   //execute this
}

Small devices Tablets (≥768px) 小型设备平板电脑(≥768px)

if (wid >= 768 && wid <= 991) {
   //execute this
}

Medium devices Desktops (≥992px) 中型设备台式机(≥992px)

if (wid >= 992 && wid <= 1199) {
   //execute this
}

Large devices Desktops (≥1200px) 大型设备台式机(≥1200px)

if (wid >= 1200) {
   //execute this
}

This is the script I always use: 这是我一直使用的脚本:

function viewport() {
    var e = window, a = 'inner';
    if (!('innerWidth' in window )) {
        a = 'client';
        e = document.documentElement || document.body;
    }
    return { width : e[ a+'Width' ] , height : e[ a+'Height' ] };
}

You can then call it to a variable, and read its height and width properties: 然后,您可以将其调用为变量,并读取其高度和宽度属性:

var vp = viewport();
var vpWidth = vp.width;
var vpHeight = vp.height;

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

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