Stack Over
Stack Over

Reputation: 37

Function isn't working on first page load

I have a function, that center some elements on page. But it not working on page first time load. It calls after page reload. How to fix it?

jQuery.fn.positionCenter = function() {
    this.css("position", "absolute");
    this.css("top", ($(window).height() / 2) - (this.outerHeight() / 2));
    this.css("left", ($(window).width() / 2) - (this.outerWidth() / 2));
    return this;
}

$(".play_btn, .around").positionCenter();
$(window).resize(function(){
    $(".play_btn, .around").positionCenter();
});

Upvotes: 0

Views: 88

Answers (2)

Stack Over
Stack Over

Reputation: 37

I fix that problem. I call after document load function.

Upvotes: -1

hsz
hsz

Reputation: 152216

Try with binding also the document.ready event:

$(document).ready(function(){
    $(".play_btn, .around").positionCenter();
});

$(window).resize(function(){
    $(".play_btn, .around").positionCenter();
});

Upvotes: 2

Related Questions