I've just solved this problem on a site I'm working on, and thought I would share it in the hope it helps someone.
My solution takes the distance from the footer to the top of the page - if the user has scrolled further than this, it pulls the sidebar back up with a negative margin.
$(window).scroll(() => {
// Distance from top of document to top of footer.
topOfFooter = $('#footer').position().top;
// Distance user has scrolled from top, adjusted to take in height of sidebar (570 pixels inc. padding).
scrollDistanceFromTopOfDoc = $(document).scrollTop() + 570;
// Difference between the two.
scrollDistanceFromTopOfFooter = scrollDistanceFromTopOfDoc - topOfFooter;
// If user has scrolled further than footer,
// pull sidebar up using a negative margin.
if (scrollDistanceFromTopOfDoc > topOfFooter) {
$('#cart').css('margin-top', 0 - scrollDistanceFromTopOfFooter);
} else {
$('#cart').css('margin-top', 0);
}
});