Reputation: 335
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>
Upvotes: 1
Views: 633
Reputation: 335
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)
Upvotes: 2