我刚刚经历了最奇怪的事情。我正在用sencha开发我的应用程序,当应用程序加载(body onload="doStuff()"
)时,我会做一个测试来检测设备的方向,我这样做:
var bodyWidth = document.body.clientWidth;
var bodyHeight = document.body.clientHeight;
var orientation = bodyHeight > bodyWidth;
alert(orientation) :
- 在桌面上:显示
true
- 在Android上:它显示
0
,这意味着错误,无论设备的方向如何,这种情况都会发生
有人能解释一下吗?!
编辑:bodyHeight
和bodyWidth
都具有正确的值。即使alert(bodyHeight > bodyWidth)
也显示出真正的
在控件中,捕获方向更改事件,如下所示:
viewport: {
//capture the orientation change event
orientationchange: 'onOrientationchange'
}
onOrientationchange: function(viewport, orientation) {
dostuff() // based on orientation
}
除此之外,你正在尝试的,你应该尝试window.innerHeight
和window.innerWidth
一个实用的JavaScript解决方案(覆盖所有浏览器):
var w = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var h = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
终于发现了问题所在:orientation
是安卓浏览器中的关键字。更改变量名称解决了问题。