/ Published in: JavaScript
Extend jQuery with a 'center' function. Use position:fixed to align center in viewport, or position:absolute inside a relatively positioned parent to center inside that parent.
Expand |
Embed | Plain Text
// Extend jQuery jQuery.fn.center = function () { this.css("position","fixed"); this.css("top", Math.max(0, (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop()) + "px"); this.css("left", Math.max(0, (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft()) + "px"); return this; } // Center a div $(element).center();
You need to login to post a comment.
