iPhoneOrientation
Demonstrates how to handle iPhone or iPod touch orientation events using HTML, CSS, and JavaScript.
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no" />
<title>Handling iPhone or iPod touch Orientation Events</title>
<!-- The iPhoneOrientation.css file is used to adjust the page appearance -->
<link rel="stylesheet" href="iPhoneOrientation.css" type="text/css" />
<!-- The iPhoneOrientation.js file shows how to handle iPhone orientation events -->
<script type="text/javascript" src="iPhoneOrientation.js"></script>
</head>
<body class="portrait">
<!-- Display a message that describes the current iPhone orientation after rotation -->
<div id="currentOrientation"></div>
<!-- The container div is made of two inner divs: leftContainer and rightContainer. The leftContainer and rightContainer are stacked
up one above the other when iPhone is in portrait orientation and side by side when iPhone is in landscape orientation -->
<div id="container">
<!--The button class is used to build a rounded rectangle around each text -->
<!--The page appearance changes whenever iPhone rotates between portrait and landscape display views -->
<div id="leftContainer">
<div class="button" onclick="window.location.href='http://developer.apple.com/documentation/AppleApplications/Reference/SafariWebContent/DebuggingSafarioniPhoneContent/chapter_10_section_1.html'">Debug Console</div>
<div class="button" onclick="window.location.href='http://developer.apple.com/documentation/AppleApplications/Reference/SafariWebContent/UsingtheViewport/chapter_4_section_8.html'">Viewport Settings</div>
</div>
<div id="rightContainer">
<div class="button" onclick="window.location.href='http://developer.apple.com/documentation/AppleApplications/Reference/SafariWebContent/AdjustingtheTextSize/chapter_5_section_4.html'">Highlighting Elements</div>
<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>
</div>
</div>
</body>
</html>
iPhoneOrientation.css
body
{
margin: 0px;
padding: 0px;
font-size: 12px;
font-family: 'Lucida Grande', Verdana, sans-serif;
font-weight: bold;
/* Turn off font resizing */
-webkit-text-size-adjust: none;
}
/* Set the background color of the page when the body tag's class attribute is equal to portrait.
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
manipulated, and "value" the class attribute's value, which is either portrait, landscapeLeft, or landscapeRight.
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
to landscapeLeft, then all style declarations matching body[class="landscapeLeft"] will be used to style "Handling iPhone or iPod touch Orientation Events".
The JavaScript updateOrientation function in the iPhoneOrientation.js file is used to set the body's class attribute to one of these values.
Further information about CSS attribute selectors can be found at http://www.w3.org/TR/css3-selectors/
*/
body[class="portrait"]
{
background: white;
}
/* Adjust a button when the body's class attribute is equal to portrait */
body[class="portrait"] .button
{
background: white;
width: 210px;
height: 13px;
padding: 10px 0px;
margin: 10px auto;
font-size: 15px;
/* text is black on the button */
color: black;
}
/* The leftContainer and rightContainer div elements are stacked up one after the other when the body's class attribute is equal to portrait.
They are both 200 pixels wide. */
body[class="portrait"] #leftContainer
{
width: 200px;
margin: auto auto;
}
body[class="portrait"] #rightContainer
{
width: 200px;
margin: auto auto;
}
/* Set the background color of the page when the body's class attribute is set to landscapeLeft */
body[class="landscapeLeft"]
{
background: lightgrey;
}
/* Adjust a button when the body's class attribute is set to landscapeLeft */
body[class="landscapeLeft"] .button
{
background: black;
}
/* The container div is evenly split between leftContainer and rightContainer when the body's class attribute is set to landscapeLeft.
leftContainer and rightContainer are stacked side by side. leftContainer aligns buttons to the left side of the page;
rightContainer aligns buttons to the right side of the page. */
body[class="landscapeLeft"] #leftContainer
{
width: 50%;
/* Align the div to the left of the page */
float: left;
}
body[class="landscapeLeft"] #rightContainer
{
width: 50%;
/* Align the div to the right of the page */
float: right;
}
/* Set the background color of the page when the body's class attribute is equal to landscapeRight */
body[class="landscapeRight"]
{
background: tan;
}
/* Adjust a button when the body's class attribute is equal to landscapeRight */
body[class="landscapeRight"] .button
{
background: darkred;
}
/* The container div is evenly split between leftContainer and rightContainer when the body's class attribute is set to landscapeRight.
leftContainer aligns buttons to the left side of the page; rightContainer aligns buttons to the right side of the page. */
body[class="landscapeRight"] #leftContainer
{
width: 50%;
float: left;
}
body[class="landscapeRight"] #rightContainer
{
width: 50%;
float: right;
}
/* Draw a rounded rectangle around a text */
.button
{
font-weight: bold;
text-align: center;
width: 130px;
height: 13px;
font-size: 10px;
/* text is white on the button */
color: white;
/* Draw a rectangle around a text */
border: 1px solid black;
/* Round each corner of the generated rectangle */
-webkit-border-radius: 5px;
padding: 5px 0px;
margin: 5px auto;
}
/* Defines styling properties for the currentOrientation div, which shows a message that indicates iPhone's current orientation after rotation */
#currentOrientation
{
width: 280px;
text-align: center;
margin: 10px auto;
}
a
{
text-decoration: none;
}
/* The container div contains four buttons that are evenly divided between the inner leftContainer and rightContainer div elements.
Its width does not change when iPhone switches between portrait and landscape orientations. */
#container
{
/* fixed width across all iPhone orientation changes */
width: 300px;
margin: 10px auto;
}
iPhoneOrientation.js
/* updateOrientation checks the current orientation, sets the body's class attribute to portrait, landscapeLeft, or landscapeRight,
and displays a descriptive message on "Handling iPhone or iPod touch Orientation Events". */
function updateOrientation()
{
/*window.orientation returns a value that indicates whether iPhone is in portrait mode, landscape mode with the screen turned to the
left, or landscape mode with the screen turned to the right. */
var orientation=window.orientation;
switch(orientation)
{
case 0:
/* If in portrait mode, sets the body's class attribute to portrait. Consequently, all style definitions matching the body[class="portrait"] declaration
in the iPhoneOrientation.css file will be selected and used to style "Handling iPhone or iPod touch Orientation Events". */
document.body.setAttribute("class","portrait");
/* Add a descriptive message on "Handling iPhone or iPod touch Orientation Events" */
document.getElementById("currentOrientation").innerHTML="Now in portrait orientation (Home button on the bottom).";
break;
case 90:
/* 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
body[class="landscapeLeft"] declaration in the iPhoneOrientation.css file will be selected and used to style "Handling iPhone or iPod touch Orientation Events". */
document.body.setAttribute("class","landscapeLeft");
document.getElementById("currentOrientation").innerHTML="Now in landscape orientation and turned to the left (Home button to the right).";
break;
case -90:
/* 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
body[class="landscapeRight"] declaration in the iPhoneOrientation.css file will be selected and used to style "Handling iPhone or iPod touch Orientation Events". */
document.body.setAttribute("class","landscapeRight");
document.getElementById("currentOrientation").innerHTML="Now in landscape orientation and turned to the right (Home button to the left).";
break;
}
}
// Point to the updateOrientation function when iPhone switches between portrait and landscape modes.
window.onorientationchange=updateOrientation;
Source: http://developer.apple.com/samplecode/iPhone/idxSafari-date.html
(
AB-D )