2017-06-01 88 views
0

以下是用於均衡幾個div的高度。在頁面加載上運行良好,不適用於窗口大小調整。腳本未在window.resize上運行

想法?

function equalHeight(group) { 
tallest = 0; 
group.each(function() { 
    thisHeight = $(this).height(); 
    if(thisHeight > tallest) { 
     tallest = thisHeight; 
    } 
}); 
group.height(tallest); 
} 

$(document).ready(function() { 
equalHeight($(".equal")); 
}); 
$(window).resize(function() { 
equalHeight($(".equal")); 
}); 

回答

0

想通了。這是因爲在調整大小時,高度已經從上次加載函數的運行中設置。我已經添加$(this).height('auto');each功能,也改變$(document).ready(function(){})$(window).load(function(){});見下圖:

function equalHeight(group) { 
    tallest = 0; 
    group.each(function() { 
     $(this).height('auto'); 
     thisHeight = $(this).height(); 
     if(thisHeight > tallest) { 
     tallest = thisHeight; 
     } 
    }); 
    group.height(tallest); 
} 
$(window).load(function() { 
    equalHeight($(".equal")); 
}); 
$(window).resize(function() { 
    equalHeight($(".equal")); 
}); 
0

試試這個

$(function equalHeight(group) { 
tallest = 0; 
group.each(function() { 
    thisHeight = $(this).height(); 
    if(thisHeight > tallest) { 
     tallest = thisHeight; 
    } 
})); 
group.height(tallest); 
} 

$(document).ready(function() { 
equalHeight($(".equal")); 
}); 
$(window).resize(function() { 
equalHeight($(".equal")); 
}); 
+0

這是行不通的。控制檯說你的語法不正確。我將第二個圓括號移至該函數的末尾,仍然不起作用 – Tom