Created
October 30, 2013 16:15
-
-
Save mikedup/7235454 to your computer and use it in GitHub Desktop.
Efficient Scroll Events
This file contains hidden or 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
$(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