$(window).load($(function()
{
var elem = $("#scroller");
var top = elem.offset().top;
var maxTop = $("#footer").offset().top - elem.height();
var scrollHandler = function()
{
var scrollTop = $(window).scrollTop();
if (scrollTop<top) {
elem.css({position:"relative",top:""})//should be "static" I think
} else if (scrollTop>maxTop) {
elem.css({position:"absolute",top:(maxTop+"px")})
} else {
elem.css({position:"fixed",top:"0px"})
}
}
$(window).scroll(scrollHandler);scrollHandler()
}));
fiddle: http://fiddle.jshell.net/3ATzd/2/show/
Only think is it doesn't like the margin-top on #scroller. I also removed the need for the scroller-anchor element, so you can remove this.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…