Skip to content

Instantly share code, notes, and snippets.

@mikedup
Created October 30, 2013 16:15
Show Gist options
  • Save mikedup/7235454 to your computer and use it in GitHub Desktop.
Save mikedup/7235454 to your computer and use it in GitHub Desktop.
Efficient Scroll Events
$(document).ready(function() {
// Fixed sidebar
var scrolled = false;
var scrollPos;
var sidebarPos = $('#sidebar').offset().top;
$(window).scroll(function() {
scrolled = true;
});
setInterval(function() {
if (scrolled) {
scrolled = false;
scrollPos = $(window).scrollTop();
if (scrollPos >= sidebarPos) {
$('#sidebar').addClass('fixed');
//console.log('Sidebar fixed');
}
else {
$('#sidebar').removeClass('fixed');
//console.log('Sidebar absolute');
}
}
}, 250);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment