iPhone Orientation
Demonstrates how to handle iPhone or iPod touch orientation events using HTML, CSS, and JavaScript.
index.html
1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 6 <meta name="viewport" content="width=device-width, user-scalable=no" /> 7 <title>Handling iPhone or iPod touch Orientation Events</title> 8 9 <!-- The iPhoneOrientation.css file is used to adjust the page appearance --> 10 <link rel="stylesheet" href="iPhoneOrientation.css" type="text/css" /> 11 12 <!-- The iPhoneOrientation.js file shows how to handle iPhone orientation events --> 13 <script type="text/javascript" src="iPhoneOrientation.js"></script> 14 </head> 15 <body class="portrait"> 16 <!-- Display a message that describes the current iPhone orientation after rotation --> 17 <div id="currentOrientation"></div> 18 19 <!-- The container div is made of two inner divs: leftContainer and rightContainer. The leftContainer and rightContainer are stacked 20 up one above the other when iPhone is in portrait orientation and side by side when iPhone is in landscape orientation --> 21 <div id="container"> 22 <!--The button class is used to build a rounded rectangle around each text --> 23 <!--The page appearance changes whenever iPhone rotates between portrait and landscape display views --> 24 <div id="leftContainer"> 25 <div class="button" onclick="window.location.href='http://developer.apple.com/documentation/AppleApplications/Reference/SafariWebContent/DebuggingSafarioniPhoneContent/chapter_10_section_1.html'">Debug Console</div> 26 <div class="button" onclick="window.location.href='http://developer.apple.com/documentation/AppleApplications/Reference/SafariWebContent/UsingtheViewport/chapter_4_section_8.html'">Viewport Settings</div> 27 </div> 28 <div id="rightContainer"> 29 <div class="button" onclick="window.location.href='http://developer.apple.com/documentation/AppleApplications/Reference/SafariWebContent/AdjustingtheTextSize/chapter_5_section_4.html'">Highlighting Elements</div> 30 <div class="button" onclick="window.location.href='http://developer.apple.com/documentation/AppleApplications/Reference/SafariWebContent/DesigningForms/chapter_7_section_1.html'">Form Auto Correction</div> 31 </div> 32 </div> 33 34 </body> 35 </html>
iPhoneOrientation.css
1 2 body 3 { 4 margin: 0px; 5 padding: 0px; 6 font-size: 12px; 7 font-family: 'Lucida Grande', Verdana, sans-serif; 8 font-weight: bold; 9 /* Turn off font resizing */ 10 -webkit-text-size-adjust: none; 11 } 12 13 14 15 /* Set the background color of the page when the body tag's class attribute is equal to portrait. 16 The expression body[class="value"] is a CSS attribute selector where "body" is the html element to be styled, "class" the body's attribute to be 17 manipulated, and "value" the class attribute's value, which is either portrait, landscapeLeft, or landscapeRight. 18 This expression is used to dynamically select a body style according to the class attribute value. For instance, if the body's class attribute is set 19 to landscapeLeft, then all style declarations matching body[class="landscapeLeft"] will be used to style "Handling iPhone or iPod touch Orientation Events". 20 The JavaScript updateOrientation function in the iPhoneOrientation.js file is used to set the body's class attribute to one of these values. 21 Further information about CSS attribute selectors can be found at http://www.w3.org/TR/css3-selectors/#attribute-selectors. 22 */ 23 body[class="portrait"] 24 { 25 background: white; 26 } 27 28 29 /* Adjust a button when the body's class attribute is equal to portrait */ 30 body[class="portrait"] .button 31 { 32 background: white; 33 width: 210px; 34 height: 13px; 35 36 padding: 10px 0px; 37 margin: 10px auto; 38 39 font-size: 15px; 40 41 /* text is black on the button */ 42 color: black; 43 } 44 45 46 /* The leftContainer and rightContainer div elements are stacked up one after the other when the body's class attribute is equal to portrait. 47 They are both 200 pixels wide. */ 48 body[class="portrait"] #leftContainer 49 { 50 width: 200px; 51 margin: auto auto; 52 } 53 54 55 body[class="portrait"] #rightContainer 56 { 57 width: 200px; 58 margin: auto auto; 59 } 60 61 62 /* Set the background color of the page when the body's class attribute is set to landscapeLeft */ 63 body[class="landscapeLeft"] 64 { 65 background: lightgrey; 66 } 67 68 69 /* Adjust a button when the body's class attribute is set to landscapeLeft */ 70 body[class="landscapeLeft"] .button 71 { 72 background: black; 73 } 74 75 76 /* The container div is evenly split between leftContainer and rightContainer when the body's class attribute is set to landscapeLeft. 77 leftContainer and rightContainer are stacked side by side. leftContainer aligns buttons to the left side of the page; 78 rightContainer aligns buttons to the right side of the page. */ 79 body[class="landscapeLeft"] #leftContainer 80 { 81 width: 50%; 82 /* Align the div to the left of the page */ 83 float: left; 84 } 85 86 87 body[class="landscapeLeft"] #rightContainer 88 { 89 width: 50%; 90 /* Align the div to the right of the page */ 91 float: right; 92 } 93 94 95 96 /* Set the background color of the page when the body's class attribute is equal to landscapeRight */ 97 body[class="landscapeRight"] 98 { 99 background: tan; 100 } 101 102 103 /* Adjust a button when the body's class attribute is equal to landscapeRight */ 104 body[class="landscapeRight"] .button 105 { 106 background: darkred; 107 } 108 109 110 /* The container div is evenly split between leftContainer and rightContainer when the body's class attribute is set to landscapeRight. 111 leftContainer aligns buttons to the left side of the page; rightContainer aligns buttons to the right side of the page. */ 112 body[class="landscapeRight"] #leftContainer 113 { 114 width: 50%; 115 float: left; 116 } 117 118 body[class="landscapeRight"] #rightContainer 119 { 120 width: 50%; 121 float: right; 122 } 123 124 125 /* Draw a rounded rectangle around a text */ 126 .button 127 { 128 font-weight: bold; 129 text-align: center; 130 131 width: 130px; 132 height: 13px; 133 font-size: 10px; 134 135 /* text is white on the button */ 136 color: white; 137 138 /* Draw a rectangle around a text */ 139 border: 1px solid black; 140 141 /* Round each corner of the generated rectangle */ 142 -webkit-border-radius: 5px; 143 padding: 5px 0px; 144 margin: 5px auto; 145 } 146 147 148 /* Defines styling properties for the currentOrientation div, which shows a message that indicates iPhone's current orientation after rotation */ 149 #currentOrientation 150 { 151 width: 280px; 152 text-align: center; 153 margin: 10px auto; 154 } 155 156 157 a 158 { 159 text-decoration: none; 160 } 161 162 163 /* The container div contains four buttons that are evenly divided between the inner leftContainer and rightContainer div elements. 164 Its width does not change when iPhone switches between portrait and landscape orientations. */ 165 #container 166 { 167 /* fixed width across all iPhone orientation changes */ 168 width: 300px; 169 margin: 10px auto; 170 }
iPhoneOrientation.js
1 2 /* updateOrientation checks the current orientation, sets the body's class attribute to portrait, landscapeLeft, or landscapeRight, 3 and displays a descriptive message on "Handling iPhone or iPod touch Orientation Events". */ 4 function updateOrientation() 5 { 6 /*window.orientation returns a value that indicates whether iPhone is in portrait mode, landscape mode with the screen turned to the 7 left, or landscape mode with the screen turned to the right. */ 8 var orientation=window.orientation; 9 switch(orientation) 10 { 11 12 case 0: 13 /* If in portrait mode, sets the body's class attribute to portrait. Consequently, all style definitions matching the body[class="portrait"] declaration 14 in the iPhoneOrientation.css file will be selected and used to style "Handling iPhone or iPod touch Orientation Events". */ 15 document.body.setAttribute("class","portrait"); 16 17 /* Add a descriptive message on "Handling iPhone or iPod touch Orientation Events" */ 18 document.getElementById("currentOrientation").innerHTML="Now in portrait orientation (Home button on the bottom)."; 19 break; 20 21 case 90: 22 /* 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 23 body[class="landscapeLeft"] declaration in the iPhoneOrientation.css file will be selected and used to style "Handling iPhone or iPod touch Orientation Events". */ 24 document.body.setAttribute("class","landscapeLeft"); 25 26 document.getElementById("currentOrientation").innerHTML="Now in landscape orientation and turned to the left (Home button to the right)."; 27 break; 28 29 case -90: 30 /* 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 31 body[class="landscapeRight"] declaration in the iPhoneOrientation.css file will be selected and used to style "Handling iPhone or iPod touch Orientation Events". */ 32 document.body.setAttribute("class","landscapeRight"); 33 34 document.getElementById("currentOrientation").innerHTML="Now in landscape orientation and turned to the right (Home button to the left)."; 35 break; 36 } 37 38 } 39 40 // Point to the updateOrientation function when iPhone switches between portrait and landscape modes. 41 window.onorientationchange=updateOrientation;
Source: http://developer.apple.com/samplecode/iPhone/idxSafari-date.html
( AB-D )