I'm facing a challenge on my website where I want a sidebar on the left to stay with the user as they scroll. The sidebar should scroll along with the user until it's 5px from the top of the page, at which point it should remain fixed in place.
In some cases, the viewport may be too small to fit the entire sidebar on the screen. This isn't a major issue, but I would like the sidebar to reach the footer of the page when the user scrolls to the bottom, and then scroll along with the page again.
I've included the code for my site and an attempt to achieve the first part of my question (although it's not currently working): jsfiddle.
The first part of the question should be clear, but the second part might be a bit confusing. Here's a mockup to help visualize it:
Here's the javascript code I've used for the first part:
$(document).ready(function () {
var theLoc = 5;
var links = $('#left');
$(window).scroll(function () {
console.log('scroll');
if (theLoc >= $(window).scrollTop()) {
if (links.hasClass('fixed')) {
links.removeClass('fixed');
}
} else {
if (!links.hasClass('fixed')) {
links.addClass('fixed');
}
}
});
});
It might also be a CSS issue:
.fixed {
position: fixed;
}
I've tried adjusting the height, but it didn't seem to make a difference.