I recently needed a sticky footer. Having a sticky footer means your footer sticks to the bottom of your browser window if your document (content) is not as heigh as your browser window.
There's a few options to do this using CSS. But they all assume you know the height (fixed) of your footer. I didn't. The footer was managed in the CSS, so I could expand or shrink over time.
I decided to write something myself. CSS only didn't seem too cut the chase. So I headed over to my jQuery toolbox and came up with the following code. You can see an example of it here. You can see it in action on a production (Drupal) site on www.encantar.be.
<div id="wrapper">
<div id="header">This is the header.</div>
<p>Some content.</p>
<div id="footer">This is the (sticky) footer.</div>
</div>
$(document).ready(function() {
positionFooter();
$(window)
.scroll(positionFooter)
.resize(positionFooter);
function positionFooter() {
var docHeight = $(document.body).height() - $("#sticky-footer-push").height();
if(docHeight < $(window).height()){
var diff = $(window).height() - docHeight;
if (!$("#sticky-footer-push").length > 0) {
$("#footer").before('<div id="sticky-footer-push"></div>');
}
$("#sticky-footer-push").height(diff);
}
}
});
As a plugin this becomes
$("#footer").stickyFooter();
// sticky footer plugin
(function($){
var footer;
$.fn.extend({
stickyFooter: function(options) {
footer = this;
positionFooter();
$(window)
.scroll(positionFooter)
.resize(positionFooter);
function positionFooter() {
var docHeight = $(document.body).height() - $("#sticky-footer-push").height();
if(docHeight < $(window).height()){
var diff = $(window).height() - docHeight;
if (!$("#sticky-footer-push").length > 0) {
$(footer).before('<div id="sticky-footer-push"></div>');
}
$("#sticky-footer-push").height(diff);
}
}
}
});
})(jQuery);
An example of the plugin version can be found here.
Note: Just make sure you run this on document.ready!
Comments
Awesome. Works perfectly in IE/FF/Safari/Opera
Hi, just want to say that there is actually a pure CSS solution for flexible sticky footers out there (http://pixelsvsbytes.com/blog/2011/09/sticky-footers-the-flexible-way/), but your way might be a good workaround for IE7 and below.
Hi,
I've got it working correctly however for some reason whenever I am on a page longer, than the browser viewable area, the div #sticky-footer-push div isn't being calculated properly and is too short.
Any help?
It does not work on Opera.
Hey dude, your plug-in is really cool, just one suggestion, add the .onload() event on window, because as it's now it doesn't the work on refresh window, so just add it like this: $(window).scroll(positionFooter).resize(positionFooter).load(positionFooter);
Thanks.
Hi
Realy nice Jquery plugin!! But i found a little bug...i mean, your plugin isn´t working on Opera Browser!!!
Just to let you know!!! :)
hai,
Thanks for sharing this info.It will be useful for the projects which needs the sticky footer.
Thanks
theme developer
Thanks for sharing. I was looking for this for a long time. And a lot of the pre made footer bars are extremely ugly so they were not an option to use.
Post new comment