(function($){ $.fn.center = function() { var fixed = this.css("position") === "fixed"; this.css("top", ($(window).height() - this.height()) / 2 + (fixed ? 0 : $(window).scrollTop()) + "px"); this.css("left", ($(window).width() - this.width()) / 2 + (fixed ? 0 : $(window).scrollLeft()) + "px"); return this; }; }(jQuery));