Archive for the ‘mobile’ Category
|Fixed Elements and Scrolling Divs in iOS 5
Friday, November 11th, 2011
I’ve been playing around with creating a web app for iOS and testing some of the new features in mobile safari that were released with iOS 5. Fixed divs and the ability to scroll a div are common in iOS native apps but were difficult to implement in a web app. These have both been added in the new safari but they still require some work-arounds to make them function like you probably want.
Fixed Elements
The first versions of iOS didn’t support fixed elements. They would fix correctly to the screen, but then they would scroll along with the rest of your content. In iOS 5, fixed elements (e.g. position: fixed; bottom: 0;) work as expected.
Keep in mind you’ll want to use the iOS meta tags if you’re designing specifically for the device. The first line below prevents the default scaling for sites that aren’t built for to adapt to the screen size and the second allows you to save the site and run it without the browser bars.
<meta name="viewport" content="initial-scale = 1.0, user-scalable = no">
<meta name="apple-mobile-web-app-capable" content="yes" />
The first issue with the fixed elements is that your site will still have the ‘bounce’ effect when scrolling past the top/bottom of your content that reveals the browser chrome (background beneath your page as it bounces). If your site doesn’t need to scroll this is a very simple fix, just add a line to prevent the default action when moving your finger:
<script type="text/javascript">
document.addEventListener('touchmove', function(event) {
event.preventDefault();
}, false);
</script>
If you want scrolling areas, but don’t want the browser chrome to show then continue on!
Scrolling Divs
Another new feature in iOS 5+ is divs can finally scroll. To make a div scroll you’ll just need to add the following (this example has a fixed header but adjust the positioning to fit your app):
.scrollable {
position: absolute;
top: 50px;
left: 0;
right: 0;
bottom: 0;
overflow: scroll;
-webkit-overflow-scrolling: touch;
}
The trick here is adding the -webkit-overflow-scrolling to anywhere you’d normally set your overflow. The downside of Apple’s implementation is that the div doesn’t bounce as you’d hope when you are already at the absolute top or bottom. Instead of bouncing the div being scrolled, it bounces the whole page and reveals the browser chrome. This is where you’ll need a javascript fix.
Fixing the Scrolling Element
Thanks to the development community there are several great options out there that you can implement. These include Scrollability and iScroll. My issue with iScroll was that it doesn’t play well with form elements if you have them in your app. At the time of this article, Scrollability’s author says it isn’t ready for release usage, but it seems to be a great solution if you want to test it. If you want your app to work well with old implementations of iOS you’ll need something similar to this (plus you’ll also need to code around the fixed elements issue), but for now we’re only addressing iOS 5+ and we’ll will save fallbacks for a different article.
The solution I like the best for iOS5 is also the simplest. Joe Lambert’s ScrollFix increases the page slightly when at the top or bottom to make your div bounce instead of the whole browser. It’s very simple to add to an element that you already have scrolling:
<script type="text/javascript" src="scrollfix.js"></script>
<script type="text/javascript">
var scrollingContent = document.getElementById("myScrollingDiv");
new ScrollFix(scrollingContent);
</script>
If you have multiple scrolling divs just apply this again to those divs. For my app I’ve got several divs which either shown or hidden and each has this script applied. We’re almost there, but two possible issues still exist. When scrolling on our fixed elements and our scrolling elements that don’t have enough content to overflow the page, the browser chrome is still revealed.
Preventing Fixed Headers from Showing Browser Chrome
This is a pretty simple fix, similar to what was described above. In my fixed header I’ve got multiple elements, so I made sure that each element or it’s parent had the class “noBounce” and then applied the following script:
document.addEventListener('touchmove', function(event) {
if(event.target.parentNode.className.indexOf('noBounce') != -1
|| event.target.className.indexOf('noBounce') != -1 ) {
event.preventDefault(); }
}, false);
This prevents the default bounce when dragging those classed elements, but doesn’t break the div that you want to scroll.
Adapting for Elements That Don’t Overflow
When creating my code some of the divs for my main content overflowed the browser window which triggers the scrollbar and bounce fix, but others didn’t. There are several ways to address this, but the easiest solution I came up with just extends all divs so they have at least a bit of scroll. To do that I added this jQuery script (you could do something similar in straight JS as well) to all my content divs:
<div class="scrollable">
<div class="contentWrapper">
Your content
</div>
</div>
<script type="text/javascript">
$('.contentWrapper').css('min-height', $(window).height() - 55 + 'px');
</script>
You’ll need to adjust the “- 55″ portion based on your application. I have it in there to prevent it from having too much scroll because of my fixed header.
Here’s a really basic demo if you want to test it out in your mobile browser: kylejlarson.com/files/iosdemo/
Posted in CSS, HTML, Javascript, mobile | 4 Comments »
How to Setup Your Site for the iPhone
Friday, October 22nd, 2010
I’ve been working on some mobile apps lately and realizing how simple it is to get started making your site more iPhone compatible. You can either create a separate site specifically for mobile or create a mobile stylesheet to be applied to specific devices. There are some great arguments out there for each option, but what I’m discussing here is just the introductory steps that work for either method. In writing this I’m also considering how I’d like to apply those ideas to my site here.
If you’re planning to create an additional stylesheet rather than a completely new codebase, you’ll want to start by specifying the stylesheet to use with a media query. Ethan Marcotte has a great article called Responsive Web Design that expands upon this idea on A List Apart. To target an iPhone/iPod specifically you can use this code right after your main stylesheet in your header:
<link media="only screen and (max-device-width: 480px)" href="iphone.css"
type="text/css" rel="stylesheet">
Control the Page Size
Next you’ve got a handful of settings that will help make your site similar to a native iPhone app. If you want your page to resize to the device, you can use this line in your header:
<meta name = "viewport" content = "width = device-width">
or if you’re setting your page width in CSS and would prefer to use a specific scale (with or without the option to disallow users from zooming the page) you can use:
<meta name = "viewport" content = "initial-scale = 1.0, user-scalable = no">
Add a Custom Icon & Startup Image
There are also meta tags for use when your site is added to the home screen of a user’s device (press the ‘+’ in mobile safari to add to the home screen). The first is adding a custom icon to your site that I wrote a previous post about. In addition, you can add a startup screen to give an app like feel. To add a startup image create a png that is 320 x 460 pixels and add this line of code to the header:
<link rel="apple-touch-startup-image" href="/your_image.png">
Make it Look Like an App
You can make your site look more like an app when people save it to their home screen by hiding the top bar that loads with safari and changing the color of the status bar. Before you do this you’ll want to make sure you’re site is setup so it can be easily navigated without needing the back button. To remove the Safari controls add this meta tag to your header:
<meta name="apple-mobile-web-app-capable" content="yes" />
And to change the status bar (network name, time, battery level) from the default grey to either “black” or “black-translucent”:
<meta name="apple-mobile-web-app-status-bar-style" content="black">
Note that if you change the status bar to translucent the site will sit underneath it. Make sure that there is no critical content hidden behind the bar.
These simple lines of code are a great way to get started setting up your site for Apple’s devices. If you’d like to go into some more detail, Apple’s Safari Reference Library is a great place to get started. Then to take this to the next level I’d recommend digging into jQTouch or Sencha. Both of these tools will create an even more app-like experience with animated transitions and app-like bars & buttons using javascript (which you’ll want to have a strong knowledge of to use to their full potential).
Tags: Apple, html, iPhone
Posted in HTML, mobile | 1 Comment »
Adding an Icon for iPhone, iPad & Android to Your Website
Friday, September 3rd, 2010
Many sites currently add a favicon before launch that is visible in your user’s browser tabs and bookmarks, but many sites are still missing icons for iOS. By adding an iOS icon, anyone who decides to save your webpage to the home screen of their iPhone, iPad, or Android* will see a nice app-like icon rather than an image of your page.
The first step is creating an image. To support the newest high resolution iPhone you’ll want to create the image at 114 x 114 pixels or larger (the device will resize the image to the correct dimensions). You can save your image as a PNG (if you plan to use transparency) or a JPG and place it in any directory on your site.
![]()
Next you’ll just add a simple bit of code to the HEAD of your site so the devices can find your image:
<link rel="apple-touch-icon" href="images/your_ios_icon.png" />
By default Apple devices will add rounded corners, a drop shadow, and reflection to your icon. If you don’t want those styles added or you’d prefer to add your own styles to the image file (like I did for my icon) you can use apple-touch-icon-precomposed instead.
<link rel="apple-touch-icon-precomposed" href="images/your_ios_icon.png" />
If you’re creating a precomposed image you can find templates for use around the web; here’s a good template from cocoia with Photoshop effects you can turn on/off.
* Note on Android Devices: versions 1.5 & 1.6 work with the “-precomposed” images only and 2.1 & new will read the tag without “-precomposed”.
Posted in HTML, mobile | 2 Comments »