3

我正在使用这个 js 使两个 div 具有相同的高度,这适用于静态内容,但是当动态加载图像/内容时,高度被设置为 0px,因为我想脚本在内容被拉出之前触发. 有没有办法将其延迟几秒钟以设置正确的高度?尝试了超时功能,但无法使其正常工作

// make stuff the same height
equalheight = function (container) {

var currentTallest = 0,
 currentRowStart = 0,
 rowDivs = new Array(),
 $el,
 topPosition = 0;
$(container).each(function () {

    $el = $(this);
    $($el).height('auto')
    topPostion = $el.position().top;

    if (currentRowStart != topPostion) {
        for (currentDiv = 0; currentDiv < rowDivs.length; currentDiv++) {
            rowDivs[currentDiv].height(currentTallest);
        }
        rowDivs.length = 0; // empty the array
        currentRowStart = topPostion;
        currentTallest = $el.height();
        rowDivs.push($el);
    } else {
        rowDivs.push($el);
        currentTallest = (currentTallest < $el.height()) ? ($el.height()) :     (currentTallest);
    }
    for (currentDiv = 0; currentDiv < rowDivs.length; currentDiv++) {
        rowDivs[currentDiv].height(currentTallest);
    }
});
}


$(window).load(function () { equalheight('.equal'); });
$(window).resize(function () { equalheight('.equal'); });
4

2 回答 2

0

您的答案没有提供足够的信息来提供明确的答案,我将开枪并假设您setTimeout在调用函数的位置尝试过,如果是这种情况,那么请尝试setTimeout像这样在函数本身中使用。

// make stuff the same height

equalheight = function (container) {
    window.setTimeout(function () {  // Start setTimeout

        var currentTallest = 0,
            currentRowStart = 0,
            rowDivs = new Array(),
            $el,
            topPosition = 0;
        $(container).each(function () {

            $el = $(this);
            $($el).height('auto')
            topPostion = $el.position().top;

            if (currentRowStart != topPostion) {
                for (currentDiv = 0; currentDiv < rowDivs.length; currentDiv++) {
                    rowDivs[currentDiv].height(currentTallest);
                }
                rowDivs.length = 0; // empty the array
                currentRowStart = topPostion;
                currentTallest = $el.height();
                rowDivs.push($el);
            } else {
                rowDivs.push($el);
                currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);
            }
            for (currentDiv = 0; currentDiv < rowDivs.length; currentDiv++) {
                rowDivs[currentDiv].height(currentTallest);
            }
        });
    }, 5000); // End setTimeout
};
于 2013-07-31T12:31:29.203 回答
-1

为什么当你选择你的元素时,你会这样做:

$el = $(this);
$($el).height('auto');

您无法选择选择器。你只需要写:

$el = $(this);    
$el.height('auto');

我不知道我这么说是不是很明显,但这似乎很奇怪。

然后对于您的问题,您应该尝试在 ajax 成功中设置高度。并检查您的选择器以确保它是正确的。

于 2013-07-31T12:14:57.047 回答