Created
November 16, 2014 19:14
-
-
Save magadanskiuchen/b718df258268e5b6a0d2 to your computer and use it in GitHub Desktop.
semi-fixed-semi-scrolling (sidebar)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#scrollPanel { | |
position: absolute; | |
top: 0px; | |
height: auto; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Slightly modernized demo: https://codepen.io/georgehtmlburger/pen/qBqqNrq