This will make an element with id footer stick to the bottom of the window, but only if the document body height is less than the window height. If it isn't it 'll just follow the normal document flow.
No margin,border or padding should be set on the element, only on its children if need be, for this to work properly:
$(function(){ positionFooter(); function positionFooter(){ if($(document).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) });
$("#pageFooterOuter").css({position: "absolute",top:($(window).scrollTop()+$(window).height()-$("#pageFooterOuter").height())+"px"})
To:
$('#pageFooterOuter').css({position:'absolute',bottom:0})
Now the footer clean move from fixed to relative and everything is more mac website style (the shopping cart on the left on the custom configuration).
jQuery(function(){
var $ = jQuery;
positionFooter();
function positionFooter(){
var winH = $(window).height();
var docH = $(document).height();
if( docH <= winH ) {
$('#content').css('margin-bottom','80px');
$("#footer").css({'position': "fixed",'bottom':'0'})
} else {
$('#content').css('margin-bottom','20px')
$("#footer").css({'position': "relative"})
}
}
jQuery(window)
.resize(positionFooter)
});
position absolute goes wrong when the contents show up and the page is not refreshed.
Most of sticked footer solutions based on css layout, but it not always possible.
So Thank you again, it save my time.
Please change this:
$(messageHolder).css('bottom', 0);
To:
$(messageHolder).css({position:'absolute',bottom:0});
try this : http://www.codicode.com/art/the_best_sticky_footer.aspx
http://www.handycss.com/tutorials/creating-sticky-footer-in-css/
This means that if you include scrolltop into the calculation, the page will always get bigger if a user scrolls.
I had to add an extra line of code which takes into account if the document.height is equal to the window height he rules out the scrolltop.
my code:
var scrollTop = ( $(window).height() < $(document).height() ) ? $(window).scrollTop() : 0;
var pos = ( $(window).height() - node.bottom.height() + scrollTop );
theElement.css( 'top', pos );
http://www.hardcode.nl/images/TTest.php
http://www.hardcode.nl/images/TTest.php
http://www.hardcode.nl/images/TTest.php
http://www.hardcode.nl/images/TTest.php
http://www.hardcode.nl/images/TTest.php
http://www.hardcode.nl/images/TTest.php
http://www.hardcode.nl/images/TTest.php