Thursday, October 7, 2010

jQuery sticky footer

No margin,border or padding should be set on the element, only on it's children if need be, for this to work properly:


$(function(){
positionFooter();
function positionFooter(){
if($(document.body).height() < $(window).height()){
$("#pageFooterOuter").css({position: "absolute",top:($(window).scrollTop()+$(window).height()-$("#pageFooterOuter").height())+"px"})
}
}

$(window)
.scroll(positionFooter)
.resize(positionFooter)
});


Without the body height conditional the footer will always stick to the bottom of the window, regardless of the body height:


$(function(){
positionFooter();
function positionFooter(){
$("#pageFooterOuter").css({position: "absolute",top:($(window).scrollTop()+$(window).height()-$("#pageFooterOuter").height())+"px"})
}

$(window)
.scroll(positionFooter)
.resize(positionFooter)
});