Skip to content

Instantly share code, notes, and snippets.

@kotarok
Created January 16, 2011 12:22
Show Gist options
  • Save kotarok/781743 to your computer and use it in GitHub Desktop.
Save kotarok/781743 to your computer and use it in GitHub Desktop.
Make footer stick to the bottom edge. Cares not to overlap upper content. Make sure that base ancestor element's position is 'relative.' I believe that mostly its 'body' element.
/*!
* jquery.stickyfooter.js
*
* Copyright 2011, Kotaro Kokubo
* kotaro@nodot.jp
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
// In jQuery1.4.4, .outerHeight() or .height doesn't work collecly sometimes.
$.fn.stickyfooter = function(){
this.each(function(){
var elem = $(this);
var positionFooter = function(){
var footerHeight = elem.outerHeight(),
contentHeight = 0,
footerTop = 0;
if(elem.css('position') == 'absolute'){
contentHeight = $(document.body).height()+footerHeight;
}else{
contentHeight = $(document.body).height();
}
footerTop = ($(window).scrollTop()+$(window).height()-footerHeight)+"px";
if (contentHeight < $(window).height()) {
elem.css({
position: "absolute",
top: footerTop
//bottom: 0
});
} else {
elem.css({
position: "static"
});
}
};
$(positionFooter);
$(window)
.load(positionFooter)
.scroll(positionFooter)
.resize(positionFooter);
});
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment