作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
window.onresize = function(){ updateOrientation(e); }
function updateOrientation(e) {
alert("hey");
deviceWidth = $('body').width();
if (deviceWidth == 320) {
currentOrientation = "portrait";
}
else if (deviceWidth == 480) {
currentOrientation = "landscape";
}
// fire a function that checks the orientation every x milliseconds
setInterval(checkOrientation, 500);
// check orientation
function checkOrientation() {
deviceWidth = $('body').width();
if (deviceWidth >= '1200') {
newOrientation = "portrait";
}
else if (deviceWidth <= '900') {
newOrientation = "landscape";
}
// if orientation changed since last check, fire either the portrait or landscape function
if (newOrientation != currentOrientation) {
if (newOrientation == "portrait") {
changedToPortrait();
}
else if (newOrientation == "landscape") {
changedToLandscape();
}
currentOrientation = newOrientation;
}
}
// landscape stuff
function changedToLandscape() {
alert('Orientation has changed to Landscape!');
}
// portrait stuff
function changedToPortrait() {
alert('Orientation has changed to Portrait!');
}
}
我正在使用它来使用phonegap检测Android平板电脑中的方向变化。不知怎的,代码不起作用。有什么建议么?我猜测它无法生成方向改变事件。
最佳答案
这对我在 PhoneGap 中有用:
function onOrientationChange() {
switch (window.orientation) {
case -90:
case 90:
alert('landscape');
break;
default:
alert('portrait');
break;
}
};
window.onresize = onOrientationChange;
关于javascript - 音隙方向变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7803578/
我是一名优秀的程序员,十分优秀!