javascript - 设置最大高度而不溢出

标签 javascript jquery html css

一些背景:我有一个 div,其中将添加不同高度的元素,我需要实现以下目标:

div 有一个 max-height 属性,当添加到 Div 的不同元素重叠这样的高度时,我不能让 div “溢出(在上面放一个滚动条)”。相反,我需要检测这种情况何时发生,因此我可以创建另一个 div,我可以在其中放置其余元素。附件是一张图片,我希望它能说明我正在尝试做的事情。

enter image description here

最佳答案

使用 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/

相关文章:

javascript - 在 jQuery 中获取元素的值

javascript - 如何在 Angular JS 中正确使用 ng-cloak?

javascript - blueimp jquery fileup 与 requriejs 不工作

javascript - 将数组对象显示到 html 表

javascript - Uncaught ReferenceError $ is not defined after new function

php - file_put_contents 在使用 json_encode 时返回 null

javascript - jPlayer - "ready"事件在 IE 7 中未触发

HTML 导入到 WordPress,设置发布日期以匹配文件名

javascript - Riot.js 编译器/404 标签未找到

javascript - 如何使用表单的变量引用来访问 jquery 中的表单字段?