Animate to Center

Animate an element to the center of the screen.

HTML

<div class="divi"> asdf </div>

CSS

.divi { width: 50px; margin: 0 auto }

JavaScript

jQuery.fn.center = function () {
    this.css("position","absolute");
    var top = ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px",
        left = ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px";
    this.animate({/*top: top, */left: left}, function(){
        $(this).css({position: 'static', margin: '0 auto'});
    });
    
    return this;
}
$('div.divi').center();