Fixed positioning in Mobile Safari

Sophie Alpert picture Sophie Alpert · Apr 13, 2009 · Viewed 110.3k times · Source

Is it possible to position an element fixed relative to the viewport in Mobile Safari? As many have noted, position: fixed doesn't work, but Gmail just came out with a solution that almost is what I want – see the floating menu bar on the message view.

Getting real-time scroll events in JavaScript would also be a reasonable solution.

Answer

Abhijit Kalamkar picture Abhijit Kalamkar · Aug 18, 2010

This fixed position div can be achieved in just 2 lines of code which moves the div on scroll to the bottom of the page.

window.onscroll = function() {
  document.getElementById('fixedDiv').style.top =
     (window.pageYOffset + window.innerHeight - 25) + 'px';
};