【问题标题】:match div heights after page load jQuery页面加载jQuery后匹配div高度
【发布时间】:2012-01-26 04:31:04
【问题描述】:

加载图像后,我无法匹配 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/

【问题讨论】:

    标签: jquery image height match


    【解决方案1】:

    在图像加载后调整列高。比如:

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

    【讨论】:

    • 稍微摆弄了一下,我得到了这个工作,谢谢!这对我有用:function matchColHeights(col1, col2) { $('img').load(function() { var col1Height = $(col1).height(); alert('col1: '+col1Height); var col2Height = $(col2).height(); alert('col2: '+col2Height); if (col1Height
    【解决方案2】:

    根据this questionwindow.load会在所有图片加载完毕后触发,所以试试这个:

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

    【讨论】:

    • 我试过这个,它似乎没有抓住 div 的高度。我做了一个警报,它甚至没有弹出。
    【解决方案3】:

    问题是图像标签上没有提供宽度和高度。 DOM 使用它们来测量,如果没有提供它们,那么它必须等待图像加载才能“重新绘制”具有正确高度的屏幕。这就是它不起作用的原因..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-09
      • 2011-09-16
      • 1970-01-01
      • 2015-12-07
      • 2013-04-18
      相关资源
      最近更新 更多