如何防止flex容器溢出/下溢
How can I prevent a flex container from overflowing/underflowing?
我有一个flexbox容器,可以通过拖动来调整大小。如何防止容器溢出(变得太小并开始隐藏某些项目)或下溢(变得太大并显示空白)?
在下面的示例中,当所有项目都达到18px
高度时(最后一个除外),容器应停止收缩,而当所有项目达到其最大高度时(不应出现空格),容器则应停止膨胀。
const resizable = document.querySelector('.flex');
let startHeight = NaN;
let startY = NaN;
resizable.addEventListener('mousedown', e => {
startHeight = resizable.getBoundingClientRect().height;
startY = e.pageY;
});
window.addEventListener('mousemove', e => {
if (isNaN(startY) || isNaN(startHeight)) {
return;
}
resizable.style.height = (startHeight + e.pageY - startY) + 'px';
});
window.addEventListener('mouseup', () => {
startHeight = startY = NaN;
});
.flex {
display: flex;
flex-direction: column;
overflow: hidden;
border: 1px solid black;
cursor: ns-resize;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.item-1 {
background: red;
height: 40px;
max-height: 60px;
flex: 1;
}
.item-2 {
background: blue;
flex: none;
}
.item-3 {
background: green;
height: 50px;
max-height: 50px;
flex: 1;
}
.item-4 {
background: yellow;
height: 30px;
flex: none;
}
<div class="flex">
<div class="item-1">
Item 1
</div>
<div class="item-2">
Item 2
</div>
<div class="item-3">
Item 3
</div>
<div class="item-4">
Item 4
</div>
</div>
最好使用纯CSS解决方案。该解决方案应该允许缩小容器,使所有物品都处于最小尺寸,并将其扩展,使所有项目都占据最大尺寸在容器的CSS中硬编码最小和最大高度是不可接受的我正在寻求一个通用的解决方案。
我认为纯CSS解决方案在这里是不可能的,因为您完全使用JavaScript设置高度。
你可以像我在下面的片段中那样添加一个保护。当可调整大小的大小为零时,以及当它很大时,您也可以计算底部高度,并在此基础上引入边界(可能性能更好,但dom变化不好)
const resizable = document.querySelector('.flex');
const lastChild = resizable.querySelector(':last-child');
const resizableBorderBottom = 1; // hardcoded - could be computed too
let startHeight = NaN;
let startY = NaN;
resizable.addEventListener('mousedown', e => {
/*
* here we assume that the bottom of the last child
* is the same as the bottom of the resizable for simplicity
*/
startHeight = resizable.getBoundingClientRect().height;
startY = e.pageY;
});
window.addEventListener('mousemove', e => {
if (isNaN(startY) || isNaN(startHeight)) {
return;
}
const lastHeight = resizable.style.height;
resizable.style.height = ((startHeight + e.pageY - startY) | 0) + 'px';
const lastChildBottom = lastChild.getBoundingClientRect().bottom | 0;
const resizableBottom = (resizable.getBoundingClientRect().bottom | 0) - resizableBorderBottom;
// check if we need to revert the change
if (lastChildBottom !== resizableBottom) {
resizable.style.height = lastHeight;
}
});
window.addEventListener('mouseup', () => {
startHeight = startY = NaN;
});
.flex {
display: flex;
flex-direction: column;
overflow: hidden;
border: 1px solid black;
cursor: ns-resize;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.item-1 {
background: red;
height: 40px;
max-height: 60px;
flex: 1;
}
.item-2 {
background: blue;
flex: none;
}
.item-3 {
background: green;
height: 50px;
max-height: 50px;
flex: 1;
}
.item-4 {
background: yellow;
height: 30px;
flex: none;
}
<div class="flex">
<div class="item-1">
Item 1
</div>
<div class="item-2">
Item 2
</div>
<div class="item-3">
Item 3
</div>
<div class="item-4">
Item 4
</div>
</div>
相关文章:
- 允许在隐藏溢出的情况下进行触摸滚动
- 当屏幕大小为中或小或xs时,文本溢出,会弄乱整个表.不过,它在全屏模式下运行良好
- 下拉菜单溢出在引导导航栏内不起作用
- 如何检测 CSS 区域中的下溢
- 当在 Ul 上使用向上/向下箭头到达溢出自动项目的顶部/底部时显示隐藏的项目
- Javascript 是否处理整数溢出和下溢?如果是,如何
- 由于列表溢出,无法在此列表左侧弹出下拉菜单(减去左边距)
- ReactJS-将溢出的元素放入一个“"下拉按钮
- 以父对象为中心并带有溢出的下拉列表:隐藏
- 从左侧和右侧计数溢出的列表项.(上/下)
- CSSJS-默认情况下,长li文本展开(溢出应隐藏)
- 什么是浮点中的溢出和下溢?
- 引导旋转木马溢出:隐藏的中断下拉按钮
- 滚动溢出下的顶部和底部潜水
- 如何溢出我的固定页脚与我的下拉列表
- 如何通过单击操作栏中溢出图标下的项目来调用其他活动
- Javascript处理整数上溢和下溢吗?如果是,如何
- 如何防止flex容器溢出/下溢
- 由于溢出属性,CSS下拉菜单没有显示
- 将溢出列表框向下移动,因为每个复选框都被循环自动选中