页面加载 jQuery 后匹配 div 高度

发布于 2024-12-22 15:17:29 字数 634 浏览 1 评论 0原文

加载图像后,我无法匹配 div 的高度。它获取最高 div 的高度,但似乎在加载图像之前就获取了它。有什么办法解决这个问题吗?这是我到目前为止所拥有的:

function matchColHeights(col1, col2) {
    var col1Height = $(col1).height();
    alert('col1 '+col1Height);
    var col2Height = $(col2).height();
    alert('col2 '+col2Height);

    if (col1Height < col2Height) {
        $(col1).height(col2Height);

    } else {
        $(col2).height(col1Height);
    }
}

$(document).ready(function() {
    matchColHeights('#leftPanel', '#rightPanel');
});

这是运行位置的链接: http://www.tigerstudiodesign .com/blog/

I am having trouble matching the height of a div after the loading of images. it gets the height of the tallest div, however it seems to get it before the images are loaded. is there any way around this? here is what I have so far:

function matchColHeights(col1, col2) {
    var col1Height = $(col1).height();
    alert('col1 '+col1Height);
    var col2Height = $(col2).height();
    alert('col2 '+col2Height);

    if (col1Height < col2Height) {
        $(col1).height(col2Height);

    } else {
        $(col2).height(col1Height);
    }
}

$(document).ready(function() {
    matchColHeights('#leftPanel', '#rightPanel');
});

here is a link to where it is being ran: http://www.tigerstudiodesign.com/blog/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹏半生如梦愿梦如真 2024-12-29 15:17:29

图像加载后是否调整列高。像这样的东西:

$('img').load(function() {
     $(col1).height(col2Height);
});

Do the column height resize after the image has loaded. Something like:

$('img').load(function() {
     $(col1).height(col2Height);
});
装迷糊 2024-12-29 15:17:29

根据 这个问题window.load 在加载所有图像时触发,因此请尝试以下操作:

$(window).load(function() {
  // ...
}

According to this question, window.load is fired when all images are loaded, so try this:

$(window).load(function() {
  // ...
}
怀中猫帐中妖 2024-12-29 15:17:29

问题是图像标签上未提供宽度和高度。 DOM 使用它们进行测量,如果未提供它们,则必须等待图像加载才能以正确的高度“重新绘制”屏幕。这就是为什么它不起作用..

The issue is when the width and the height are not provided on the images tags. The DOM uses them to measure, if they aren't provided then it has to wait for the image to load in order to "re paint" the screen with the correct heights. This is why it wasn't working..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文