如何以编程方式找到Phonegap / JQuery手机中的设备宽度 [英] How to programmatically find the device-width in Phonegap/JQuery mobile

查看:154
本文介绍了如何以编程方式找到Phonegap / JQuery手机中的设备宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到移动设备的设备宽度。
虽然我们可以指定内容

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?

推荐答案

视口通过窗口对象收集:

var viewport = {
    width  : $(window).width(),
    height : $(window).height()
};

//can access dimensions like this:
//viewport.height


$ b b

虽然您不会总是得到完美的结果,但是不同的设备会有不同的行为,这会给予视口尺寸,而不是屏幕尺寸。

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 (因为它设置为 device-width 100%):

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手机中的设备宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆