Skip to content

Instantly share code, notes, and snippets.

@craigbutcher
Last active August 29, 2015 14:25
Show Gist options
  • Save craigbutcher/3f53b6168f7bf3e45fed to your computer and use it in GitHub Desktop.
Save craigbutcher/3f53b6168f7bf3e45fed to your computer and use it in GitHub Desktop.
Fixed sidebar scrolling with stick to bottom when reach... the bottom of the page.
// Using require.js
if(has('.fixed-sidebar')) {
// Global variable to find the correct value where fixed-sidebar resides at
var setGlue = $('.fixed-sidebar').offset().top;
$(document).scroll(function(){
// Fetch the value from the browser's window
var windowTop = $(window).scrollTop();
// Add the glue when the sidebar's value is less than the browser's window
// Else if the sidebar's value is greater, remove the glue
if (setGlue < windowTop) {
$('.fixed-sidebar').addClass('glue');
// console.log('adding the floating glue');
} else if (setGlue > windowTop) {
$('.fixed-sidebar').removeClass('glue');
// console.log('removing the floating glue');
}
// Testing for when document window hits the top
//if($(document).scrollTop() == 0 ){
// console.log("top");
//}
// When we reach the bottom of the page
//
// If the document top + browser's window is greater than document height - 100
// Add class 'glue-to-bottom'
// Else when the user scrolls up, remove the class.
if ($(document).scrollTop() + $(window).height() > $(document).height() - 100) {
$('.fixed-sidebar').addClass('glue-to-bottom');
} else if (setGlue < windowTop) {
$('.fixed-sidebar').removeClass('glue-to-bottom');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment