A little dab'll do ya
Code Snippets
jQuery Sticky Footer
In general the CSS Sticky Footer is the best way to go, as it works perfectly smoothly and doesn't require JavaScript. If the markup required simply isn't possible, this jQuery JavaScript might be an option.
HTML
Just make sure the #footer is the last visible thing on your page.
<div id="footer">
Sticky Footer
</div>CSS
Giving it a set height is the most fool-proof.
#footer { height: 100px; }jQuery
When the window loads, and when it is scrolled or resized, it is tested whether the pages content is shorter than the window's height. If it is, that means there is white space underneath the content before the end of the window, so the footer should be repositioned to the bottom of the window. If not, the footer can retain it's normal static positioning.
// Window load event used just in case window height is dependant upon images
$(window).bind("load", function() {
var footerHeight = 0,
footerTop = 0,
$footer = $("#footer");
positionFooter();
function positionFooter() {
footerHeight = $footer.height();
footerTop = ($(window).scrollTop()+$(window).height()-footerHeight)+"px";
if ( ($(document.body).height()+footerHeight) < $(window).height()) {
$footer.css({
position: "absolute"
}).animate({
top: footerTop
})
} else {
$footer.css({
position: "static"
})
}
}
$(window)
.scroll(positionFooter)
.resize(positionFooter)
});
Seems to have a few kinks… appears to queue animations if you keep resizing and dragging the window size larger and smaller?
you could either just add a .stop(); before the animation. Or, don’t use .animate at all, just set the top value with the .css function. Just a little jerky that way.
I am using your demo files that already has the stop() in it and the animation is till happening.
How can I stop it at all and just have the footer stay at the bottom of the page?
$footer.css({
position: “absolute”
}).stop().animate({
top: footerTop
})
} else {
$footer.css({
position: “static”
})
}
sorry, but it is not very smooth… what’s about:
{ position: fixed; bottom: 0; width: 100%; }
That is completely against the whole point of this. This STOPS before the bottom of the content, so it doesn’t overlap any content. Fixing it to the bottom will happily lay over the top of content.
That is completely against the whole point of this. This STOPS before the bottom of the content, so it doesn’t overlap any content. Fixing it to the bottom will happily lay over the top of content.
Mac 10.5.8, Safari 4.0.3… this is incredibly buggy. Resizing the window makes the footer shift (a good thing), but it doesn’t sit flush with the bottom of the window, instead having a big white border show thru. Eww.
Seems to be running fine for me with the same exact specs. I just added the .stop() before the animate, which might help. I think Safari was just firing a shit-load of resize events and you computer was probably having trouble to keep up.
Seems to be working fine for me.
OS X 10.6.2 using Chrome 4.0.2, Safari 4.0.4 and FF 3.5.5; keep up the good work.
It did get a little jumpy but it seems to be more my computer than the code itself.
Works great and looks great for me on Safari, FF, and Chrome on Snow Leopard.
Chris, you do good stuff. Keep it up!
you could achieve the same result entirely through CSS, utilizing 100% heights properly along with absolute positioning… the resulting CSS would actually be more “liquid” when resizing a window, and thus not have the buggy effect of the footer jumping around after the window has been resized. the important thing to pay attention to, is to get the footer to NOT overlap your content, but rather force a scroll when it hits the bottom of that container (which this demo succeeds at)…. example of pure CSS version: http://oribe.com/
Hi this is cool….how did u do this plz give me code for that..thnx
I usually just stick to CSS when I’m doing a “sticky footer” but this is a cool example, thanks!
Cool example. I like to use this pure CSSsticky footer. Pretty similar to your example but with an empty push div.
Buenisimo Chris!
Era justo lo que estaba buscando.
Mil gracias desde Argentina.
Mauricio
Awesome Chris!
It’s just what I was looking for.
Thanks a lot from Argentina.
Mauricio
This method has some bugs when using zoom, but it’s good when too lazy to make css trick.
And advantage of that method is that it works on footer without defined height (some sites have long lists in footer and you cannot use height for it).
Note that you will have to use width:100% for footer, because position:absolute shrinks width to content. And you may also have to use footer wrapper, if you want to center it.
I love it footers have always gave me issues
it’s nice thanks.
Fantastic, thank you it is very nice
This is excellent thanks so much. Is there away to make it start from the bottom rather than slide down from the top when the script loads?