如何以编程方式在 Phonegap/JQuery mobile 中查找设备宽度 [英] How to programmatically find the device-width in Phonegap/JQuery mobile
问题描述
我需要找到移动设备的设备宽度.虽然我们可以指定内容
I need to find the device-width of the mobile device. Although we can specify the content
<meta name="viewport" content="width=device-width; user-scalable=no" />
但我需要以编程方式获取设备宽度来计算我的应用程序中的一些逻辑.如何获取设备宽度?
But i need to programmatically obtain the device width to compute some logic in my application. How can I get the device width?
推荐答案
viewport
维度可以通过 window
对象收集:
The viewport
dimensions can be gathered via the window
object:
var viewport = {
width : $(window).width(),
height : $(window).height()
};
//can access dimensions like this:
//viewport.height
虽然你不会总是得到完美的结果,但不同的设备表现不同,这给出了 viewport
尺寸,而不是屏幕尺寸.
Though you won't always get perfect results, different devices behave differently and this gives the viewport
dimensions, not the screen dimensions.
或者,您可以检查 data-role="page"
元素的宽度以找到 device-width
(因为它设置为 100%<
device-width
的/code>):
Alternatively you could check the width of a data-role="page"
element to find the device-width
(since it's set to 100%
of the device-width
):
var deviceWidth = 0;
$(window).bind('resize', function () {
deviceWidth = $('[data-role="page"]').first().width();
}).trigger('resize');
这篇关于如何以编程方式在 Phonegap/JQuery mobile 中查找设备宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!