一些背景:我有一个 div,其中将添加不同高度的元素,我需要实现以下目标:
div 有一个 max-height 属性,当添加到 Div 的不同元素重叠这样的高度时,我不能让 div “溢出(在上面放一个滚动条)”。相反,我需要检测这种情况何时发生,因此我可以创建另一个 div,我可以在其中放置其余元素。附件是一张图片,我希望它能说明我正在尝试做的事情。
最佳答案
使用 jQuery:
var maxHeight = $(".someElement").css("max-height");
var height = 0;
$(".elements").each(function(){
height += $(this).height();
if(height >= maxHeight){
//create new div here and put the rest of the elements there
height = 0; //so you can continue with the loop and creating more divs.
}
});
关于javascript - 设置最大高度而不溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26919414/