Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling to selected element while typing on iphone safari virtual keyboard

I am building an iphone webapp and have a page with an input field. I'd like the field to scroll up to just above the virtual keyboard when it shows up. I've tried putting a scrollTo(x,y) on the input focus event (i.e. just before the keyboard shows up), but when I start typing the page scrolls up again (presumably based on the default mobile safari behavior). I also tried setting keypress event handlers, but preventing propagation of those events just disabled the keyboard, although it did prevent scrolling.

Is there any way to force the page to be at a particular position (with the input field just above the keyboard) when the virtual keyboard shows up, and not have it move when i resume typing?

like image 434
spinlock Avatar asked Jun 01 '10 15:06

spinlock


People also ask

How do you scroll on iPhone keyboard?

By tapping and holding the spacebar on an iPhone's keyboard, users can turn it into a touchpad that can be used to scroll through texts, like an actual touchpad found on laptops.

How do you make fixed content go above iOS keyboard?

To force it work the same way as Mobile Chrome, you have to use position: absolute, height: 100% for the whole page or a container for your pseudo-fixed elements, intercept scroll, touchend, focus, and blur events. The trick is to put the tapped input control to the bottom of screen before it activates focus.


1 Answers

Ok, so this is probably the worst hack you will ever see, but it beats nothing.

What you could do is change the position of the text box dynamically (using javascript) to fixed, position:fixed, this will change the position of the textbox to be relative to the browser window instead of the page. This should render the Iphone's scrolling irrelevant and your text box should be at the top of the page no matter what. Then onBlur, the css position is set to relative again (which should put it back in its place).

To make this prettier you could put a div behind the textbox onFocus so it hides the actual site content, and you could center the textbox using the top and left css properties (just make sure to clear those too onBlur).

like image 99
Tomas Reimers Avatar answered Sep 19 '22 05:09

Tomas Reimers