iPhone window.onorientationchange Code

Written by on December 25th, 2007 in Ajax News.

Re-posted from devphone.com.

iPhone Snow Globe

Apple has put up some sample code that shows off the new window.onorientationchange and window.orientation ability that lets you detect the orientation of the iPhone.

You setup your HTML with a body tag with the class of “portrait” or “landscape” and most of the CSS goes from that. You also put an empty div with the id of currentOrientation that is used for some of the magic.

Below is a simple JavaScript handler:

JAVASCRIPT:

  1.  
  2. window.onorientationchange = function() {
  3.   /*window.orientation returns a value that indicates whether iPhone is in portrait mode, landscape mode with the screen turned to the
  4.     left, or landscape mode with the screen turned to the right. */
  5.   var orientation = window.orientation;
  6.   switch(orientation) {
  7.     case 0:
  8.         /* If in portrait mode, sets the body’s class attribute to portrait. Consequently, all style definitions matching the body[class="portrait"] declaration
  9.            in the iPhoneOrientation.css file will be selected and used to style "Handling iPhone or iPod touch Orientation Events". */
  10.         document.body.setAttribute(”class”,”portrait”);
  11.        
  12.         /* Add a descriptive message on "Handling iPhone or iPod touch Orientation Events"  */
  13.         document.getElementById(”currentOrientation”).innerHTML=”Now in portrait orientation (Home button on the bottom).”;
  14.         break; 
  15.        
  16.     case 90:
  17.         /* If in landscape mode with the screen turned to the left, sets the body’s class attribute to landscapeLeft. In this case, all style definitions matching the
  18.            body[class="landscapeLeft"] declaration in the iPhoneOrientation.css file will be selected and used to style "Handling iPhone or iPod touch Orientation Events". */
  19.         document.body.setAttribute(”class”,”landscapeLeft”);
  20.        
  21.         document.getElementById(”currentOrientation”).innerHTML=”Now in landscape orientation and turned to the left (Home button to the right).”;
  22.         break;
  23.    
  24.     case -90: 
  25.         /* If in landscape mode with the screen turned to the right, sets the body’s class attribute to landscapeRight. Here, all style definitions matching the
  26.            body[class="landscapeRight"] declaration in the iPhoneOrientation.css file will be selected and used to style "Handling iPhone or iPod touch Orientation Events". */
  27.         document.body.setAttribute(”class”,”landscapeRight”);
  28.        
  29.         document.getElementById(”currentOrientation”).innerHTML=”Now in landscape orientation and turned to the right (Home button to the left).”;
  30.         break;
  31.   }
  32. }
  33.  

And, since it is Christmas/insert your winter holiday here, there is a nice sample snow globe that you can shake by changing the orientation of your iPhone.

Have a great holiday period, and try to stay off of your phones and spend some time with the family ;)

Source: Ajaxian
Original Article: http://feeds.feedburner.com/~r/ajaxian/~3/206122584/iphone-windowonorientationchange-code

Comments are closed.



Site Navigation