[英]Equal Heights JS
Good evening, 晚上好,
I have been using a javascript to generate equal height columns using the following JS: 我一直在使用JavaScript使用以下JS生成相等高度的列:
<script type="text/javascript">
var maxHeight = 0;
$(".level").each(function(){
maxHeight = $(this).height() > maxHeight ? $(this).height() : maxHeight;
}).height(maxHeight);
</script>
However, I wanted to be able to add additional classes and was advised to amend my code to that shown below. 但是,我希望能够添加其他类,并建议将我的代码修改为以下所示。 One other reason was to prevent the script creating a global variable like the one above. 另一个原因是阻止脚本像上面那样创建全局变量。
Now the problem is, the first class works but the additional ones don't seem to be. 现在的问题是,第一堂课有效,但其他的似乎没有。 They are generating a height but not an equal height. 它们产生的高度不是相等的高度。 Can anyone help me work out the problem? 谁能帮我解决问题?
<script type="text/javascript">
(function() {
function equalHeights(selector) {
var maxHeight = 0;
function calcEqualHeight() {
var el = $(this);
maxHeight = el.height() > maxHeight ? el.height() : maxHeight;
el.height(maxHeight);
}
selector.each(calcEqualHeight);
}
equalHeights($('.level-1'));
equalHeights($('.level-2'));
equalHeights($('.level-3'));
})();
</script>
You need to set the height after you complete the loop: 完成循环后,您需要设置高度:
(function() {
function equalHeights(selector) {
var maxHeight = 0;
function calcEqualHeight() {
var el = $(this);
maxHeight = el.height() > maxHeight ? el.height() : maxHeight;
}
selector.each(calcEqualHeight).height(maxHeight);
}
equalHeights($('.level-1'));
equalHeights($('.level-2'));
equalHeights($('.level-3'));
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.