I understand that mobile safari has a lot of bugs around fixed elements, but for the most part I've managed to get my layout working correctly until I added a much needed text input to the fixed navigation at the bottom. Now when the user focuses on the text input element and the virtual keyboard appears, my navigation, which is otherwise always fixed at the bottom of the page, jumps up to a really strange spot in the middle of the page.
I'd add some of my code to this post, but I wouldn't be sure where to start. That navigation is fixed at the bottom and positioned to the left and bottom 0, and 100% width. From there, I don't know what's going on, I can only assume it's a mobile safari bug.
It also appears to lose it's position fixed and become relative, only while the text input element is focused on and the virtual keyboard is open.
Test this one. It works. I just test it.
http://dansajin.com/2012/12/07/fix-position-fixed/ this is one of the solutions proposed. Seems worth a shot.
In short: set
fixed
elements toposition:absolute
when any input isfocus
ed and reset them when that element isblur
redand
I wasn't having any luck with the solution proposed by Dan Sajin. Perhaps the bug has changed since he wrote that blog post, but on iOS 7.1, the bug will always surface when the position is changed back to fixed after the input is blurred, even if you delay until the software keyboard is hidden completely. The solution I came to involves waiting for a
touchstart
event rather than theblur
event since the fixed element always snaps back into proper position when the page is scrolled.HTH
The
focusin
andfocusout
events seem to be better suited to this problem than thefocus
andblur
events since the former bubble up to the root element. See this answer on SO.Personally I use AngularJS, so I implemented it like this:
Note: I am conditionally running this script if this is mobile Safari.
I put an
ng-class
attribute on my navbar:using the following CSS:
You can read more about
focusin
here andfocusout
here.Taking from what sylowgreen did, the key is to fix the
body
on entering theinput
. Thus:I really like the solution above. I packaged it up into a little jQuery plugin so I could:
Code example: