Hi coders,
I'd like to keep a div element within the viewport when page is being scrolled. I'm currently using this snippet using jquery:
$(function() { var offset = $("#column-menu").offset(); var topPadding = 25; $(window).scroll(function() { if ($(window).scrollTop() > offset.top) { $("#column-menu").stop().animate({ marginTop: $(window).scrollTop() - offset.top + topPadding }); } else { $("#column-menu").stop().animate({ marginTop: 25 }); }; }); });
It works great but what happens is that the element disappear upon scrolling and then comes down from the top
-- what I would like instead -- The element stops as soon as its top border reaches the top of the viewport, no animation, no gliding no nuttin'.
It should be ie6,ie7 and ie8 compliant... Any suggestions (even with prototype) would be great. Thanks.