From 93c5ad118e31cfa07b02eef48e3ef7378f2d5150 Mon Sep 17 00:00:00 2001 From: John Bender Date: Mon, 26 Sep 2011 14:03:28 -0700 Subject: [PATCH] fix for incorrect portrait of lanscape value The value attached to the event passed into handlers was based soley on screensize which is problematic given that some implementations (eg Android 2.3) don't change the screensize until after the event is fired. The orientation property appears to report a better value where it is provided so the solution is to prefer what it reports and then fallback to the screensize caculation where necessary. --- js/jquery.mobile.event.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/js/jquery.mobile.event.js b/js/jquery.mobile.event.js index 6a58cefa..d0e105f5 100644 --- a/js/jquery.mobile.event.js +++ b/js/jquery.mobile.event.js @@ -242,8 +242,22 @@ $.event.special.swipe = { // Get the current page orientation. This method is exposed publicly, should it // be needed, as jQuery.event.special.orientationchange.orientation() $.event.special.orientationchange.orientation = get_orientation = function() { - var elem = document.documentElement; - return elem && elem.clientWidth / elem.clientHeight < 1.1 ? "portrait" : "landscape"; + var isPortrait = true, elem = document.documentElement; + + // prefer window orientation to the calculation based on screensize as + // the actual screen resize takes place before or after the orientation change event + // has been fired depending on implementation (eg android 2.3 is before, iphone after). + // More testing is required to determine if a more reliable method of determining the new screensize + // is possible when orientationchange is fired. (eg, use media queries + element + opacity) + if ( $.support.orientation ) { + // if the window orientation registers as 0 or 180 degrees report + // portrait, otherwise landscape + isPortrait = window.orientation % 180 == 0; + } else { + isPortrait = elem && elem.clientWidth / elem.clientHeight < 1.1; + } + + return isPortrait ? "portrait" : "landscape"; }; })( jQuery, window );