Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm creating a jQTouch app in PhoneGap for the iPhone. I've been battling with this issue for days. I've seen the eventlistener solution suggested a few times, but just could not get it to work.</p> <p>In the end I came up with a different solution. It basically checks the width of the body periodically using settimeout. If the width is 320 then the orientation is portrait, if 480 then landscape. Then, if the orientation has changed since the last check, it will fire either a portrait stuff function or a landscape stuff function where you can do your thing for each orientation.</p> <p>Code (note, I know there is some repetition in the code, just haven't bothered to trim it down yet!):</p> <pre><code>// get original orientation based on body width 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 == '320') { newOrientation = "portrait"; } else if (deviceWidth == '480') { 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!'); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload