Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
This course surveys the core principles and techniques essential to building web sites for mobile devices. Author Joe Marini introduces the mobile context, sheds light on its unique coding requirements, and discusses interface design techniques that enhance existing sites for mobile viewing. The course shows how to approach designing for mobile form factors such as smaller screens and finger-based interaction, along with how to incorporate CSS3 and HTML5 capabilities, such as geolocation, local storage, and media queries.
On the iPhone, your web pages can also detect changes in the device orientation, and you do that by responding to orientation events. The orientation events look like this. First, there is the onorientationchange, which is the event that's fired on the body element. You can assign a function to that handler to respond to changes in the orientation. You can also look at the orientation property on the window to see what the current orientation is. Now, it should be pointed out that window.orientation is read only.
You can't programmatically set the orientation and in the onorientationchange event, there is no way to prevent it from happening. The orientation is going to change, regardless of what your code tries to do. So let's take a look at example of handling this event to see how we can respond to device orientation changes in our code. So here I am in Xcode, and I've got my orientation_start example open, and this is just a very simple orientation example. So what I am going to do is have a function here called updateOrientation, and in this little div down here with the ID of output, I am just going to show what the current orientation is.
So I am going to go over my snippets file, and you can see I am scrolled down to the simple orientation example. The first thing I am going to do is just copy the onorientationchange handler and paste that onto the code here for the body. So now I've got the onorientationchange handler. The other thing I am going to do is, in onload, I am going to call updateOrientation as well. That way, that div has some initial value, and we're not just getting a blank white screen. So I am going to save that.
Now I am going to copy over the code that actually handles the updating of the orientation. That's this function right here. So I want to copy this, scroll down right there, and I am going to copy and I am going to paste, and I am going to paste it right inside this function here. So, let's take a quick look at the code. You can see that I've got a variable called displayString and that says Orientation. And then based on what the value of the window's orientation is, whether it's 0 or -90, or 90 or 180 degrees, I'm going to update the displayString in the output div to match the string that I've constructed here.
So as I change the device orientation, it will say things like Orientation Portrait or Orientation Landscape. So let's go ahead and save that, and let's try it out. So I'll go over to the Simulator, and you can see that I've currently got the file loaded. And just to show you how it works, in the Hardware menu, you can do things like Rotate Left and you can do Rotate Right. Now, this is the initial state of the file. So let me just copy over the edited one to the web server, and I'll copy over orientation_start.
Yes, I'll replace it, okay, and go back to the simulator. Now, I am going to reload it, and you can see when I reload it, the window's onload handler gets called, and the current state of the device is in Portrait mode, so it's showing 0 degrees, and we'll move it over here. So now I am going to rotate it to the left and you can see that now the orientation is at landscape, which is 90 degrees counterclockwise, and I'll rotate it right and then right again. And now you can see that the landscape is at -90 degrees. Now, I tried doing upside down, but apparently the iOS Simulator doesn't like the upside-down setting, but there is a 180 setting that your application can detect, just so you know.
But it looks like the Simulator is not doing that. But anyway, using the window.orientation and the onorientationchange event, your web applications and web pages can respond to changes in the way the hardware is oriented.
There are currently no FAQs about Mobile Web Design & Development Fundamentals.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.