Skip to content

Instantly share code, notes, and snippets.

@craigbutcher
Last active August 29, 2015 14:25
Show Gist options
  • Save craigbutcher/4e5d1efe464c331fc27e to your computer and use it in GitHub Desktop.
Save craigbutcher/4e5d1efe464c331fc27e to your computer and use it in GitHub Desktop.
javascript module for sticky sidebar
// Sticky sidebar - JavaScript
'use strict';
var fixedSidebar = {
init: function() {
// Set up the click handlers.
this.bindUIActions();
},
bindUIActions: function() {
$(window).scroll(function(){
fixedSidebar.scrolling();
fixedSidebar.topOfPage();
});
},
scrolling : function() {
var setGlue = $('.sidebar').offset().top;
var windowTop = $(window).scrollTop();
var topPadding = 15;
// var documentHeight = $(document).scrollTop() + $(window).height() > $(document).height() - 100;
// console.log(documentHeight);
// if (documentHeight) {
// $('.sidebar').addClass('glue-to-bottom');
// } else if (setGlue < windowTop) {
// $('.sidebar').removeClass('glue-to-bottom');
// }
if (windowTop > setGlue) {
$('.sidebar').addClass('glue');
} else if (windowTop < setGlue) {
$('.sidebar').removeClass('glue');
}
},
topOfPage : function() {
if ($(document).scrollTop() == 0) {
$('.sidebar').removeClass('glue');
}
}
}
$(document).ready(function() {
fixedSidebar.init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment