Fixed position on desktop browsers, relative/static position on mobile devices (Android, iOS ...)

Adyyda

A few days ago i run into a problem that i did not imagine that exists regarding fixed position. I have searched and i have found a few articles regarding this (best described is http://bradfrost.com/blog/mobile/fixed-position/ ) and it seems that no viable solution exist.

On short, i want to have a fixed nav and a side block as you can see bellow (the code works perfect on desktop browsers (Chrome, Firefox, IE8) ), that shows and works like that only on desktop browsers. If a mobile device browser is used like Android, iOS, Windows Mobile eg), the fixed position for nav and sidebar should be changed in static or relative, to make those 2 blocks scroll up with the rest of the page.

Solutions via media queries are not ok because they target device screen and not actual device/browser.

Can this be done? I would appreciate a jsfiddle to test it with mobile devices. Thanks

var win      = $(window),
    fxel     = $('nav'),
    eloffset = fxel.offset().top;

win.scroll(function() {
    if (eloffset < win.scrollTop()) {
        fxel.addClass("fixed");
    } else {
        fxel.removeClass("fixed");
    }
});

/*!
 * StickySidebar jQuery Plugin v1.0.1
 * Copyright 2014 Dawid Pawelec
 * Released under the MIT license
 */

(function ($) {
    var $w = $(window);

    $.fn.stickySidebar = function (options) {
        var o = $.extend({}, $.fn.stickySidebar.defaults, options),
            $c = $(o.container);

        return this.each(function () {
            var $s = $(this),
                sh = $s.outerHeight(),
                originalMarginTop = parseInt($s.css('margin-top'), 10),
                top = $s.offset().top - o.offsetTop - originalMarginTop,
                bottom = $c.offset().top + $c.outerHeight() - o.offsetTop,

                handleScroll = function () {
                    var scroll = $w.scrollTop();
                    if (scroll > top) {
                        if (scroll + sh + o.offsetBottom > bottom && o.bottom) {
                            $s.removeClass(o.stuckClass);
                            $s.addClass(o.bottomClass);
                        } else {
                            $s.css('margin-top', o.offsetTop + originalMarginTop);
                            $s.addClass(o.stuckClass);
                        }
                    } else {
                        $s.css('margin-top', originalMarginTop);
                        $s.removeClass(o.stuckClass);
                        $s.removeClass(o.bottomClass);
                    }
                };

            $w.on('scroll', handleScroll);
        });

    };

    $.fn.stickySidebar.defaults = {
        stuckClass: 'fixed',
        bottomClass: 'bottom',
        container: '.container',
        offsetTop: 80,
        offsetBottom: 0,
        bottom: true
    };

}(jQuery));



// Usage
$('.sidebar').stickySidebar({
    container: '.container',
    offsetBottom: 5
});
.header, .footer {
    background: #ddd;
    padding: 15px;
    border-radius: 5px
}
.header-top {height:50px;}
.header-bottom {height:60px;}
.header {
    margin-bottom: 5px;
    height: 120px;
}

.container {
    background: #ddd;
    margin-bottom: 5px;
    padding: 5px;
    position: relative;
    border-radius: 5px;
}

.sidebar, .main-content {
    background: #fff;
    border: 1px solid #ccc;
    padding: 15px;
}

.sidebar {
    position: absolute;
    width: 169px;
    height: 200px;
}

.sidebar.fixed {
    position: fixed;
    top: 0;
}

.sidebar.bottom {
    bottom: 5px;
}

.main-content {
    margin-left: 205px;
    width: 253px;
    height: 2000px;
    min-height: 400px;
}

.footer {
    height: 500px;
}

nav {
  height:50px;
  padding:10px;
  background-color:black;
  color:white;
}

nav.fixed {
  position:fixed;
  top:0px;
  right:0px;
  left:0px;
  z-index:999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<header class="header">
    <div class="header-top">Header</div>
    <div class="header-bottom"><nav>Sticky!</nav> </div>
    </header>
   
    <div class="container">
        <div class="sidebar">Sidebar</div>
        <div class="main-content">Main</div>
    </div>
    
    <footer class="footer">Footer</footer>

Adyyda

I managed to solve the issue by inserting all the jquery code between if( /Windows|OS X/i.test(navigator.userAgent) ) { ALL JQUERY CODE }

This way, all is working properly (tested on windows - Chrome/IE8/Firefox; osx laptop -Safari/Chrome/Firefox; Android tablet)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Fixed position not working on ios devices

From Dev

Fixed position not working on ios devices

From Dev

Fixed position div causes page to be truncated in less common mobile browsers

From Dev

Jw player not working in mobile web browsers (android & IOS devices)

From Dev

Jw player not working in mobile web browsers (android & IOS devices)

From Dev

Geolocation code working on desktop browsers but not on mobile devices

From Dev

disappearing position fixed header in ios7 mobile safari

From Dev

Position fixed not working in mobile browser

From Dev

Prevent fixed-position background-image: cover from resizing in mobile browsers upon address bar hide

From Dev

Position fixed not working as expected in some browsers

From Dev

How can I make my header position correctly across mobile and desktop devices?

From Dev

position:fixed footer in iPhone iOS

From Dev

Fixed position of responsive element in desktop mode

From Dev

Fixed position of responsive element in desktop mode

From Dev

Position:fixed breaks font-size on mobile

From Dev

jQuery Mobile Dialog Page - Fixed Position Header

From Dev

Div with position:fixed not visible in mobile Responsive design

From Dev

position:fixed does not work on android

From Dev

Fontello rendering on iOS, but not Android and desktop browsers

From Dev

iOS7 position:fixed; works ugly

From Dev

Overflow scrolling not working on position fixed element iOS

From Dev

Background scrolling behind position:fixed; on iOS

From Dev

Android Floating buttons are not set at a fixed position

From Dev

WebGL position different on desktop and android phone

From Dev

Can't click fixed position element on mobile safari

From Dev

Mobile safari position:fixed z-index glitch when scrolling

From Dev

Mobile web: -webkit-overflow-scrolling: touch conflicts with position:fixed

From Dev

Stop header footer from loosing position:fixed with click - Jquery Mobile

From Dev

Fixed position, 100% height element, not always 100% on mobile

Related Related

  1. 1

    Fixed position not working on ios devices

  2. 2

    Fixed position not working on ios devices

  3. 3

    Fixed position div causes page to be truncated in less common mobile browsers

  4. 4

    Jw player not working in mobile web browsers (android & IOS devices)

  5. 5

    Jw player not working in mobile web browsers (android & IOS devices)

  6. 6

    Geolocation code working on desktop browsers but not on mobile devices

  7. 7

    disappearing position fixed header in ios7 mobile safari

  8. 8

    Position fixed not working in mobile browser

  9. 9

    Prevent fixed-position background-image: cover from resizing in mobile browsers upon address bar hide

  10. 10

    Position fixed not working as expected in some browsers

  11. 11

    How can I make my header position correctly across mobile and desktop devices?

  12. 12

    position:fixed footer in iPhone iOS

  13. 13

    Fixed position of responsive element in desktop mode

  14. 14

    Fixed position of responsive element in desktop mode

  15. 15

    Position:fixed breaks font-size on mobile

  16. 16

    jQuery Mobile Dialog Page - Fixed Position Header

  17. 17

    Div with position:fixed not visible in mobile Responsive design

  18. 18

    position:fixed does not work on android

  19. 19

    Fontello rendering on iOS, but not Android and desktop browsers

  20. 20

    iOS7 position:fixed; works ugly

  21. 21

    Overflow scrolling not working on position fixed element iOS

  22. 22

    Background scrolling behind position:fixed; on iOS

  23. 23

    Android Floating buttons are not set at a fixed position

  24. 24

    WebGL position different on desktop and android phone

  25. 25

    Can't click fixed position element on mobile safari

  26. 26

    Mobile safari position:fixed z-index glitch when scrolling

  27. 27

    Mobile web: -webkit-overflow-scrolling: touch conflicts with position:fixed

  28. 28

    Stop header footer from loosing position:fixed with click - Jquery Mobile

  29. 29

    Fixed position, 100% height element, not always 100% on mobile

HotTag

Archive