Center Div on scroll

Div Center on Scroll

jQuery.fn.center = function () {
            this.css("position", "absolute");
            this.css("top", (($(window).height() - this.outerHeight()) / 2) +
                                                $(window).scrollTop() + "px");
            this.css("left", (($(window).width() - this.outerWidth()) / 2) +
                                                $(window).scrollLeft() + "px");
            return this;
        }
// OR
jQuery.fn.center = function () {
    $top = (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop();
    $left = (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft();
    this.css({ "position": "absolute", "top": $top + "px", "left": $left + "px" });
    return this;
}

        $(window).scroll(function () {
            $('#ajaxloader').center();
        });
<div id="ajaxloader">
Hello Center DIV on scroll
</div>
#ajaxloader{ background-color:lightblue;}​
Demo / Download
Center Div on scroll Center Div on scroll Reviewed by Bhaumik Patel on 1:32 AM Rating: 5