var $scrollingDiv = $("#content-left"); $(window).scroll(function(){ var y = $(this).scrollTop(), maxY = $('#footer').offset().top, scrollHeight = $scrollingDiv.height(); if(y< maxY-scrollHeight-100 ){ $scrollingDiv .stop() .animate({"marginTop": ($(window).scrollTop()) + "px"}, "slow" ); } });