Wednesday, November 17, 2010

There are several method to implement it. The best way is the new feature of iPhone OS 2.0.

"Fixed positioning in Mobile SafariAugust 5, 2008

11/16/2010: It's been a long time since my fixed positioning script was released. I'm very pleased that it was the precursor for much more robust solutions to this problem. Please continue on if you'd like to read through the history of this hack. If you're looking for a real-world solution please take a look at one of the following, ranked (subjectively) in order of scroll performance:

Update: An anonymous genius in the comments suggested using translateY instead of top for the animation. After some edits I've updated my demo, and it flies! The scrolling animation is smooth as silk. Apparently Webkit transforms are the only hardware-accelerated animations at this point. Thanks, random dude on the internet!

Update 2: This code is released to the public domain. You can use, modify, remix as you see fit.

Here's a video for those without iPhones. This is running in the iPhone Simulator bundled with the SDK. Note that the animation is much choppier on an actual iPhone.

With the release of iPhone OS 2.0 came some great improvements over previous Mobile Safari versions. CSS animations are in (though buggy), as well as native touch events like touchstart, touchend, gesturechange, etc.

I played around with these new goodies while hunting for improvements to build into Pickleview. The most fascinating change to me was that you can now prevent the default behavior of elements with a simple preventDefault() call. It allows a user to drag an element around the screen without having to worry about the viewport wobbling about.

I grew curious as to what I could specifically call this on, and started testing out several elements. Turns out you can preventDefault on everything in the DOM, including the body element. This seemed incredibly useless for no other reason than the terrible usability it would bring if you couldn't scroll the viewport. Then the proverbial light bulb went off: fixed positioning!

First, let's recap why fixed positioning does not work off-the-bat. Mobile Safari uses a viewport to show you websites. Imagine a book in front of you. Take a piece of paper, cut a 320×416 square in it, and lay it over the book. To read the book, move the paper around and position the hole over the words you want to see. This is exactly what Mobile Safari's viewport is doing. When you flick and scroll, you're moving the viewport around while the website behind it stays static.

This renders fixed positioning null and void on iPhone. An element that has its position fixed is affixed to the body, not the viewport. So it is actually working as intended, though most people would prefer it attached to the viewport.

There are workarounds in the wild, but these are inelegant. You can reposition an element onscroll, but a scroll event in Mobile Safari is only fired after scrolling has stopped. This results in an evident "glitch" since you have to a) flick to your desired position, throwing the element off-screen, and b) wait for the element to reappear in the viewport after scrolling has stopped.

By disabling the default scroll behavior on the body element, you essentially glue the viewport down to its initial starting point, where it's unable to go anywhere. This limits the viewport to exactly 320×416 pixels of space to show you. In this state, you have a perfectly useless experience.

This is where it gets interesting. In order to re-enable scrolling, I needed to only make the content area scrollable (think iframe, with header and footer above and below it). The touch and gesture events gives access to X/Y values, as well as timers and offset values. So by logging these and incrementing the top offset of the content area, we can create a scrollable block that does not affect the header or footer elements. A little spit and polish later: voila!

It's pretty evident from my proof-of-concept that CSS animations need a lot of work. They're promised to be "hardware-accelerated", but there's little proof of this. Most animations glitch, some to the point of non-use. The scrolling isn't particularly smooth and even something as simple as animating a 'top' CSS property takes its toll. Still it's usable, though I hope later Mobile Safari builds will address these issues.

MobileCrunchreportson a new report from Japanese research firm MMRI claiming that Apple held a 60% share of the Japanese smartphone market for the period of April through September, easily outdistancing second-place Sony Ericsson's 20.6% share.

According to MMRI, a total of 2.23 million smartphones (Android/Windows Mobile/Blackberry/iOS/Palm) were sold in Japan between April and September this year, and 1.34 million (or 60.1%) of those were iPhones.

The report notes that smartphone sales more than doubled over the year-ago period, despite the fact that overall mobile phone sales in Japan grew by only 12%. With smartphones now approaching 12% of Japan's overall smartphone sales, Apple's dominant position in that rapidly-growing smartphone market has enabled it to grab 7% of the total mobile phone market there.