Skip to main content

Fixed positioning in Mobile Safari


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.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. iOS 5 will have support for position:fixed.

    ReplyDelete
  2. 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';
    };

    ReplyDelete
  3. 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.

    ReplyDelete
  4. 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.

    ReplyDelete
  5. 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

    ReplyDelete
  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...

    ReplyDelete
  7. I found this just now: http://doctyper.com/archives/200808/fixed-positioning-on-mobile-safari/

    (This question was also in the Google search results.)

    (Sorry for the necrobump.)

    ReplyDelete
  8. I found the gmail solution for floaty bar.

    http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-css.html

    ReplyDelete
  9. This solve position fixed bug:
    www.appml.org

    ReplyDelete
  10. 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' });
    }
    };

    ReplyDelete
  11. <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.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.