Hiding address bar on mobile when scrolling child with overflow: auto

When you create a long base page and scroll through it, most mobile browsers do something automatically with the address bar. For example, on android chrome, when scrolling down, the address bar disappears with a scroll.

Can the same effect be achieved if you don't scroll the body? For example, if you have a child div containing content with height: 100%

and overflow: auto

.

+3


source to share


1 answer


If you want to hide the address bar on iPhone, you can use:

<meta name="viewport" content="width=device-width, minimal-ui">

      

There is no such simple solution for Android like iOS 7 on iPhone with meta tags.



You can try this snippet from Scott Jell to hide the panel in js targeting both iOS and Android.

/*! Normalized address bar hiding for iOS & Android (c) @scottjehl MIT License */
(function( win ){
    var doc = win.document;

    // If there a hash, or addEventListener is undefined, stop here
    if(!win.navigator.standalone && !location.hash && win.addEventListener ){

        //scroll to 1
        win.scrollTo( 0, 1 );
        var scrollTop = 1,
            getScrollTop = function(){
                return win.pageYOffset || doc.compatMode === "CSS1Compat" && doc.documentElement.scrollTop || doc.body.scrollTop || 0;
            },

            //reset to 0 on bodyready, if needed
            bodycheck = setInterval(function(){
                if( doc.body ){
                    clearInterval( bodycheck );
                    scrollTop = getScrollTop();
                    win.scrollTo( 0, scrollTop === 1 ? 0 : 1 );
                }   
            }, 15 );

        win.addEventListener( "load", function(){
            setTimeout(function(){
                //at load, if user hasn't scrolled more than 20 or so...
                if( getScrollTop() < 20 ){
                    //reset to hide addr bar at onload
                    win.scrollTo( 0, scrollTop === 1 ? 0 : 1 );
                }
            }, 0);
        }, false );
    }
})( this );

      

0


source







All Articles