开发者

Android detect rotation & screen width

开发者 https://www.devze.com 2023-03-23 13:52 出处:网络
I am trying to detect screen rotation in Webkit on my Motorola Droid 1.I have the following JavasCript code:

I am trying to detect screen rotation in Webkit on my Motorola Droid 1. I have the following JavasCript code:

var supportsOrientationChange = "onorientationchange" in window,
    orientationEvent = support开发者_运维知识库sOrientationChange ? "orientationchange" : "resize";

window.addEventListener(orientationEvent, function() {
    alert("screen.width: " + screen.width + ", window.orientation: " + window.orientation);
}, false);

The problem is: it alerts screen.width 569 when the phone is rotated in portrait, and 320 when it is rotated in landscape. This does not any sense to me! Can somebody clarify?

Thank you, Igor


I bet you are running Android 2.2. There is a bug in the browser's reporting of width and height. See screen.width + android and Shouldn't window.screen.width/height correspond to actual screen width/height? for starters.

I wish I had something better to offer than the setTimeout() workaround in that first post, but that may be the best you can do. (I'd love to be wrong about that, so if someone knows a better solution, please share!)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号