Skip to content

Instantly share code, notes, and snippets.

@kopiro
Created March 8, 2013 14:16
Show Gist options
  • Select an option

  • Save kopiro/5116710 to your computer and use it in GitHub Desktop.

Select an option

Save kopiro/5116710 to your computer and use it in GitHub Desktop.
Scrollbar fixed in JS
self.sidebarAutoScroll = function()
{
window.content = $('#page>.row>div:eq(0)');
window.sidebar = $('#page>.row>div:eq(1)');
sidebar.data('initial-offset', sidebar.offset());
sidebar.data('initial-height', sidebar.height());
if (sidebar.data('initial-height')<content.height() && sidebar.data('initial-height')<$(window).height() && $(window).width()>768) {
console.log('Using sidebarAutoScroll');
var footerOffset = $('#footer').offset().top;
$(window).scroll(function(e){
if (pageYOffset>sidebar.data('initial-offset').top-20) {
var top = Math.min(-pageYOffset-sidebar.data('initial-height')+footerOffset-40,20);
sidebar.css({
position:'fixed',
top: top,
left: window.sidebar.data('initial-offset').left-20
});
} else {
sidebar.css({
position:'static'
});
}
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment