Jquery计算.ready和.resize之间的高度差



初始化页面或调整页面大小时,高度计算似乎存在差异。

有人知道为什么吗?或者如何修复?

示例代码

$(document).ready(function () {
    showcase_height_init();
    $(window).resize(function() {
        showcase_height_init();
    });
});
function showcase_height_init() {
    var showcase_container_text_height = ($("#showcase_container_text").height())/2;
    $("#showcase_container_text").css({
        margin: '-'+showcase_container_text_height+'px 0 0'
    });
}

只要DOM可用,就会触发"ready"事件,因此可能没有加载样式、图像和其他资源等属性,并且元素的高度不是真实的。

为此,您应该尝试:

$("body").load(function () {
    showcase_height_init();
    $(window).resize(function() {
        showcase_height_init();
    });
});
function showcase_height_init() {
    var showcase_container_text_height = ($("#showcase_container_text").height())/2;
    $("#showcase_container_text").css({
        margin: '-'+showcase_container_text_height+'px 0 0'
    });
}

我推荐jquery的文档:

http://api.jquery.com/ready/

最新更新