功能不适用于首页加载

时间:2016-08-04 10:18:02

标签: javascript jquery

我有一个功能,它将页面上的一些元素集中在一起。但它没有在第一次加载页面上工作。它在页面重新加载后调用。如何解决?

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();
});

2 个答案:

答案 0 :(得分:2)

尝试绑定document.ready事件:

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

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

答案 1 :(得分:-1)

我解决了这个问题。 我在文档加载功能之后调用。

相关问题