Fixed positioning in Mobile Safari
Asked Answered
E

11

67

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.

Eritrea answered 13/4, 2009 at 5:42 Comment(0)
T
35

iOS 5 has support for position:fixed.

Teacart answered 8/6, 2011 at 0:57 Comment(7)
Saw that this morning. Finally!Eritrea
What about other mobile devices not on iOS?Iormina
@danwellman, I believe browser is called Mobile Safari in iOS only.Teacart
Why is this the accepted answer? There are still many users on lower versions of iOS fore one reason because of the missing support for there device..Sclaff
@Sclaff - because it works for iOS 5, we expect users to throw their old devices in the bin, something you have to do with Apple as most software doesn't work on older phones.Suiter
If you use position:fixed as of IOS 5.1.1 (or earlier), you're likely in for a world of pain. It's buggy. Very buggy.Crap
I've found this css-tricks.com/forums/discussion/17406/…Leg
L
74

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';
};
Linseylinseywoolsey answered 17/8, 2010 at 22:0 Comment(4)
Doesn't this present a bit of a delay?Eleen
onscroll isn't fired until after the scrolling is completed, so the element would scroll with the page and then snap to the bottom when scrolling is done.Osmond
To add to this, mobile Safari now supports hardware accelerated transforms. So element.style.webkitTransform = "translate3d(0, " + window.pageYOffset + "px, 0)"; will actually execute faster and less choppy than setting the top value.Cosgrove
@Adam: Don't know why but that didn't worked for me. The element doesn't showed up at all. Before it had position:fixed;top:50%;left:50%;.Dexterdexterity
T
35

iOS 5 has support for position:fixed.

Teacart answered 8/6, 2011 at 0:57 Comment(7)
Saw that this morning. Finally!Eritrea
What about other mobile devices not on iOS?Iormina
@danwellman, I believe browser is called Mobile Safari in iOS only.Teacart
Why is this the accepted answer? There are still many users on lower versions of iOS fore one reason because of the missing support for there device..Sclaff
@Sclaff - because it works for iOS 5, we expect users to throw their old devices in the bin, something you have to do with Apple as most software doesn't work on older phones.Suiter
If you use position:fixed as of IOS 5.1.1 (or earlier), you're likely in for a world of pain. It's buggy. Very buggy.Crap
I've found this css-tricks.com/forums/discussion/17406/…Leg
F
10

See Google's solution to this problem. You basically have to scroll content yourself using JavaScript. Sencha Touch also provides a library for getting this behavior in a very performant manor.

Fetch answered 22/12, 2010 at 18:58 Comment(1)
Link is outdated. Try web.archive.org/web/20141001100814/https://…Kelcey
S
6

it worked for me:

function changeFooterPosition() {   
  $('.footer-menu').css('top', window.innerHeight + window.scrollY - 44 + "px");
}

$(document).bind('scroll', function() {
  changeFooterPosition();
});

(44 is the height of my bar)

Although the bar only moves at the end of the scroll...

Seldom answered 1/12, 2010 at 18:35 Comment(0)
A
5

This may interest you. It's Apple Dev support page.
http://developer.apple.com/library/ios/#technotes/tn2010/tn2262/

Read the point "4. Modify code that relies on CSS fixed positioning" and you will find out that there is very good reason why Apple made the conscious decision to handle fixed position as static.

Alodium answered 29/11, 2010 at 13:40 Comment(2)
deep link: developer.apple.com/library/ios/#technotes/tn2010/tn2262/#//…Schematism
There's no "very good reason" given at that link. All that is attempted is a description of the current situation.Virgo
H
4

I think gmail just tracks the scroll position on a timer and repositions a div accordingly.

The best solution I've seen is at doctyper.

A simpler jQuery solution that moves an element onscroll: link

Hak answered 13/4, 2009 at 5:57 Comment(3)
How do you get the scroll position, then?Eritrea
I've added the simpler method that tracks onscroll. Feel free to dig through jQuery to find the exact method to use; it might be window.pageYOffset but I'm not certainHak
The Problem with doctyper is that the phone responsiveness starts to lag when there is too much content to scroll ...Immemorial
F
3

You could try using touch-scroll, a jQuery plugin that mimics scrolling with fixed elements on mobile Safari: https://github.com/neave/touch-scroll

View an example with your iOS device at http://neave.github.com/touch-scroll/

Or an alternative is iScroll: http://cubiq.org/iscroll

Fantail answered 15/2, 2011 at 15:12 Comment(1)
I used both. Both work well. iScroll 4 is more thorough if you need more than just scroll control (ie fixed positioning.) Touch Scroll is limited and smaller.Littleton
S
1

This is how i did it. I have a nav block that is below the header once you scroll the page down it 'sticks' to the top of the window. If you scroll back to top, nav goes back in it's place I use position:fixed in CSS for non mobile platforms and iOS5. Other Mobile versions do have that 'lag' until screen stops scrolling before it's set.

// css
#sticky.stick {
    width:100%;
    height:50px;
    position: fixed;
    top: 0;
    z-index: 1;
}

// jquery 
//sticky nav
    function sticky_relocate() {
      var window_top = $(window).scrollTop();
      var div_top = $('#sticky-anchor').offset().top;

      if (window_top > div_top)
        $('#sticky').addClass('stick');
      else
        $('#sticky').removeClass('stick');
     }

$(window).scroll(function(event){

    // sticky nav css NON mobile way
       sticky_relocate();

       var st = $(this).scrollTop();

    // sticky nav iPhone android mobile way iOS<5

       if (navigator.userAgent.match(/OS 5(_\d)+ like Mac OS X/i)) {
            //do nothing uses sticky_relocate() css
       } else if ( navigator.userAgent.match(/(iPod|iPhone|iPad)/i) || navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) ) {

            var window_top = $(window).scrollTop();
            var div_top = $('#sticky-anchor').offset().top;

            if (window_top > div_top) {
                $('#sticky').css({'top' : st  , 'position' : 'absolute' });
            } else {
                $('#sticky').css({'top' : 'auto' });
            }
        };
Subinfeudation answered 20/3, 2012 at 17:15 Comment(0)
C
1
<meta name="viewport" content="width=320, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/> 

Also making sure height=device-height is not present in this meta tag helps prevent additional footer padding that normally would not exist on the page. The menubar height adds to the viewport height causing a fixed background to become scrollable.

Chauncey answered 11/5, 2012 at 22:52 Comment(0)
R
0

Here you can see what (mobile) browsers support css position fixed + there version.

http://caniuse.com/css-fixed

Renaldorenard answered 12/3, 2013 at 8:37 Comment(0)
P
0

Our web app requires a fixed header. We are fortunate in that we only have to support the latest browsers, but Safari's behavior in this area caused us a real problem.

The best fix, as others have pointed out, is to write our own scrolling code. However, we can't justify that effort to fix a problem that occurs only on iOS. It makes more sense to hope that Apple may fix this problem, especially since, as QuirksMode suggests, Apple now stands alone in their interpretation of "position:fixed".

http://www.quirksmode.org/blog/archives/2013/12/position_fixed_1.html

What worked for us is to toggle between "position:fixed" and "position:absolute" depending on whether the user has zoomed. This replaces our "floating" header with predictable behavior, which is important for usability. When zoomed, the behavior is not what we want, but the user can easily work around this by reversing the zoom.

// On iOS, "position: fixed;" is not supported when zoomed, so toggle "position: absolute;".
header = document.createElement( "HEADER" );
document.body.appendChild( header );
if( navigator.userAgent.match( /iPad/i ) || navigator.userAgent.match( /iPhone/i )) {
    addEventListener( document.body, function( event ) {
        var zoomLevel = (( Math.abs( window.orientation ) === 90 ) ? screen.height : screen.width ) / window.innerWidth;
        header.style.position = ( zoomLevel > 1 ) ? "absolute" : "fixed";
    });
}
Paoting answered 22/10, 2014 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.