【问题标题】:Jquery calculate height difference between .ready and .resizeJquery计算.ready和.resize之间的高度差
【发布时间】:2013-09-08 02:04:05
【问题描述】:

在初始化页面或调整页面大小时,高度计算似乎有所不同。

有人知道为什么吗?或者如何解决?

示例代码

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

}

【问题讨论】:

    标签: jquery resize documentation height


    【解决方案1】:

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

    为此,您应该尝试:

    $("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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 2016-04-23
      • 2019-05-05
      • 1970-01-01
      • 2019-07-02
      相关资源
      最近更新 更多