views:

4690

answers:

7

Hi there;

I've got a UIWebView embedded in my iPhone app, and I'd like to keep a locked header and footer DIV on the page at all times, with a scrollable center DIV.

I know that I could do this using a header/footer that are UIView controls, but I want the header and footer to be HTML divs, as a pure HTML/JS/CSS solution will be easier to port to Android/PalmPre/AdobeAir, which is going to be on my todo list relatively soon.

I can do this using techniques like the one mentioned here:

http://defunc.com/blog/?p=94

But this requires that the user use 2 fingers to scroll the div, which is not satisfactory to me...

Any suggestions on how to do this?

Thanks,

Brad

+1  A: 

May be clunky, but you could reposition the header and footer over top of the div as the user scrolls. This way your main div doesn't need to be scrollable. No help for anything (still) using frames though.

This is one of the more irritating browser issues with the iPhone/touch, I wish you could just focus on part of the page like a normal browser.

Dana the Sane
+2  A: 

I'm not too familiar with the UIWebView, so this may be a totally silly suggestion. But is there anything stopping you from having three UIWebViews on the page? One for the header, one for the body, and one for the footer. Because breaking it up sounds like the right idea.

toast
There's nothing stopping me from doing that, but then I have to coordinate the views from Objective C. I'd prefer to keep this as pure HTML/JS/CSS as then it'd port to Android/PalmPre/AdobeAir type things way easier.
Brad Parks
That's cool. Functional requirements are just as valid as technical ones.
toast
A: 

For a CSS only reference the Safari CSS Reference probably has what you are looking for. You'll be especially interested in anything starting with "-webkit" or "-khtml" as those are extended properties only available with WebKit like 3D and touches. Should apply to Android as well.

With JavaScript the Introduction to WebKit DOM Programming Topics and WebKit DOM API Reference are go-to guides. Definately take a look at the light-table demo for some copy and paste javascript on handling your touches as that's how I would solve this.

slf
A: 

Look at this comment to find the best answer so far:

http://stackoverflow.com/questions/615357/scrollable-div-on-iphone-without-using-2-fingers/678370#678370

Brad Parks
+4  A: 

I found someone that implemented a reusable solution for this, with a header and a footer:

http://cubiq.org/scrolling-div-for-mobile-webkit-turns-3/16

Hope it helps!

Brad

Brad Parks
+2  A: 

Is this what you're looking for? Open this link on your iPhone device or simulator.

The index.html file has three div elements for "header", "container" and "footer" directly under the body, while all the work is done in the fixed.js file. The document is fixed in place by canceling the normal action for the "touchmove" event:

 // Disable flick events
 disableScrollOnBody : function() {
  document.body.addEventListener("touchmove", function(e) {
   e.preventDefault();
  }, false);
 },

Then, a lot of work goes into creating event listeners for the "touchstart", "touchmove" and "touchend" events which are attached to the "content" div under "container". The logic boils down to simply moving the "content" div up and down.

This solution is 100% HTML/CSS/JavaScript, however there is some WebKit proprietary CSS and JavaScript which may limit portability. It may take a bit of tweaking to work on another mobile device but this would be a good proof-of-concept to start from.

I did not create this awesome sample project, I'm merely bringing it to the community's attention. For more information and a link to the zipped project, read Richard "Doctyper" Herrara's entire post on Fixed positioning in Mobile Safari.

phatblat
A: 

I have implemented iScroll on iphone and it is really smooth and fast and you can do whatever you want. Disadvantages are that android (1.6) refuses to scroll how I wanted and sometimes block other javascript if there are any.

Marjan