Stop floating scrollbar when reaching to footer

Stop floating scrollbar when reaching to footer

I need to stop floating scrollbar when reaching to footer. I tried the follwing code but no luck.

var $sidebar = jQuery(".cartitemsguy"), $window = jQuery(window), offset = $sidebar.offset(), topPadding = 15; var footerwraptop = jQuery('.footer').offset.top; $window.scroll(function() { if ($window.scrollTop() > offset.top) { $sidebar.stop().animate({ marginTop: $window.scrollTop() - offset.top + topPadding }); if (jQuery('.cartitemsguy').offset().top + jQuery(".cartitemsguy").height() > jQuery(".footer").offset().top) { alert('reached to footer'); } } else { $sidebar.stop().animate({ marginTop: 0 }); } });

Thanks in advance.