Skip to content

Instantly share code, notes, and snippets.

@magadanskiuchen
Created November 16, 2014 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magadanskiuchen/b718df258268e5b6a0d2 to your computer and use it in GitHub Desktop.
Save magadanskiuchen/b718df258268e5b6a0d2 to your computer and use it in GitHub Desktop.
semi-fixed-semi-scrolling (sidebar)
jQuery(function ($) {
var $win = $(window);
var $scrollPanel = $('#scrollPanel');
var windowTop = 0;
var lastDelta = 0;
var fixed = false;
$win.scroll(function (e) {
var newTop = $win.scrollTop();
var delta = newTop - windowTop;
if (!fixed) {
if (delta > 0) {
scrollPanelBottom = parseInt($scrollPanel.css('top')) + $scrollPanel.outerHeight(true);
visibleBottom = newTop + $win.height();
if (visibleBottom > scrollPanelBottom) {
$scrollPanel.css({ position: 'fixed', top: 'auto', bottom: 0 });
fixed = true;
}
} else {
if ($scrollPanel.position().top - windowTop > 10) {
$scrollPanel.css({ position: 'fixed', top: '0px', bottom: 'auto' });
fixed = true;
}
}
} else {
if ( (delta > 0 && lastDelta < 0) || (delta < 0 && lastDelta > 0) ) {
$scrollPanel.css({ position: 'absolute', top: $scrollPanel.offset().top + 'px', bottom: 'auto' });
fixed = false;
}
}
lastDelta = delta > 0 ? 1 : -1;
windowTop = newTop;
});
});
#scrollPanel {
position: absolute;
top: 0px;
height: auto;
}
@georgeHtmlBurger
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment