我有一个包含三个元素的Flex容器。我希望前两个元素始终位于第一行,最后一个元素填充整个第二行。我需要动态更改第二个元素后的Flex-Wrap规则。是否可以?
请不要建议我改变结构。我知道前两个元素有一个内部容器是可行的,但是我不能修改结构;我只能添加CSS规则。
示例已更新。尝试缩小第一列,直到排序图标到达第二行。我希望列标题和图标保留在第一行,搜索输入保留在第二行。类似于https://swimlane.github.io/ngx-datatable/
var activeResizeHandler;
var clickEventX;
var originalWidth;
$(".resize-handle").mousedown(function(e) {
activeResizeHandler = e.target;
clickEventX = e.originalEvent.x;
originalWidth = e.target.parentNode.clientWidth;
$(e.target).addClass("active");
});
$(document).mouseup(function(e) {
activeResizeHandler = null;
$(".resize-handle.active").removeClass("active");
});
$(document).mousemove(function(e) {
if (!activeResizeHandler) return;
var newWidth = originalWidth + e.originalEvent.x - clickEventX;
console.log(newWidth);
e.target.parentNode.style.width = newWidth + 'px';
});
th {
position: relative;
}
.resize-handle {
position: absolute;
top: 0;
right: 0;
width: 1ch;
height: 100%;
}
th:hover .resize-handle,
.resize-handle.active {
visibility: visible;
border-right: 1px solid red;
cursor: col-resize;
}
.first-child {
margin-right: 1ch;
}
.second-child {}
.third-child {
flex-basis: 100%;
min-width: 0;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th scope="col">
<div class="d-flex flex-wrap">
<span class="first-child">Some unnecessarily long text to test</span>
<i class="second-child bi bi-sort-down-alt"></i>
<input type="text" class="third-child" />
</div>
<span class="resize-handle"></span></th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
1条答案
按热度按时间e5njpo681#
flex-basis
属性。flex-wrap:wrap
,它会将溢出的元素分解到下一行。chil3
将在新行中,但由于您希望它占用100%的空间,只需将flex-basis:100%
添加到child3
。flex-basis:100%
。一个二个一个一个