Had*_*rZH 5 html javascript css css3 flexbox
我用HTML/JS/CSS Flexbox实现了我自己的分割窗格.
在下列情况下我遇到分离器问题 - 其中一个面板具有固定大小(以px为单位),另一个面板设置为grow(flex-grow: 1).
如果其他面板有子尺寸,则不会滚动到最后.它会卡在儿童的大小.
这可以通过分裂窗格面板上的CSS修复,但不能修复孩子吗?
使用flex对我来说非常重要,因为我希望保持应用程序的响应能力,并希望尽可能避免使用固定大小.
这是 我的问题的JSFiddle示例.
代码片段如下.谢谢!
function startDrag() {
glass.style = 'display: block;';
glass.addEventListener('mousemove', drag, false);
}
function endDrag() {
glass.removeEventListener('mousemove', drag, false);
glass.style = '';
}
function drag(event) {
var splitter = getSplitter();
var panel = document.getElementById('c2');
var currentWidth = panel.offsetWidth;
var currentLeft = panel.offsetLeft;
panel.style.width = (currentWidth - (event.clientX - currentLeft)) + "px";
}
function getSplitter() {
return document.getElementById('splitter');
}
var con = document.getElementById('container');
var splitter = document.createElement('div');
var glass = document.getElementById('glass');
splitter.className = 'splitter';
splitter.id = 'splitter';
con.insertBefore(splitter, con.lastElementChild);
splitter.addEventListener('mousedown', startDrag, false);
glass.addEventListener('mouseup', endDrag, false);Run Code Online (Sandbox Code Playgroud)
.container {
display: flex;
border: 1px solid;
width: 500px;
height: 300px;
position: absolute;
}
.c1 {
background-color: blue;
flex: 1;
height: 100%;
}
.c2 {
background-color: green;
width: 150px;
}
.splitter {
width: 20px;
cursor: col-resize;
}
.glass {
height: 100%;
width: 100%;
cursor: col-resize;
display: none;
position: absolute;
}
.grandchild {
background-color: red;
width: 50px;
height: 50px;
}Run Code Online (Sandbox Code Playgroud)
<div id="container" class="container">
<div id="glass" class="glass"></div>
<div class="c1">
<div class="grandchild"></div>
</div>
<div id="c2" class="c2"></div>
</div>Run Code Online (Sandbox Code Playgroud)
它卡在孩子们的大小上
这是使用 时的预期行为flexbox。我想如果你想滚动到最后,那么你可以使用position: absolute相grandchild对于c1:
.grandchild {
background-color: red;
width: 50px;
height: 50px;
position: absolute;
top: 0;
left: 0;
}
Run Code Online (Sandbox Code Playgroud)
overflow: hidden也给c1:
.c1 {
background-color: blue;
flex: 1;
height: 100%;
position: relative;
overflow: hidden;
}
Run Code Online (Sandbox Code Playgroud)
干杯!
.grandchild {
background-color: red;
width: 50px;
height: 50px;
position: absolute;
top: 0;
left: 0;
}
Run Code Online (Sandbox Code Playgroud)
.c1 {
background-color: blue;
flex: 1;
height: 100%;
position: relative;
overflow: hidden;
}
Run Code Online (Sandbox Code Playgroud)
function startDrag() {
glass.style = 'display: block;';
glass.addEventListener('mousemove', drag, false);
}
function endDrag() {
glass.removeEventListener('mousemove', drag, false);
glass.style = '';
}
function drag(event) {
var splitter = getSplitter();
var panel = document.getElementById('c2');
var currentWidth = panel.offsetWidth;
var currentLeft = panel.offsetLeft;
panel.style.width = (currentWidth - (event.clientX - currentLeft)) + "px";
}
function getSplitter() {
return document.getElementById('splitter');
}
var con = document.getElementById('container');
var splitter = document.createElement('div');
var glass = document.getElementById('glass');
splitter.className = 'splitter';
splitter.id = 'splitter';
con.insertBefore(splitter, con.lastElementChild);
splitter.addEventListener('mousedown', startDrag, false);
glass.addEventListener('mouseup', endDrag, false);Run Code Online (Sandbox Code Playgroud)
解决方案:
所以我想你的策略应该是使用grandchild填充整个侧面板的绝对值,然后将其放入content内部,如下所示:
<div class="grandchild">
<div class="content"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
并更改这些样式:
.grandchild {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.grandchild .content{
background-color: red;
width: 50px;
height: 50px;
}
Run Code Online (Sandbox Code Playgroud)
下面的例子:
.container {
display: flex;
border: 1px solid;
width: 500px;
height: 300px;
position: absolute;
}
.c1 {
background-color: blue;
flex: 1;
height: 100%;
position: relative;
overflow: hidden;
}
.c2 {
background-color: green;
width: 150px;
}
.splitter {
width: 20px;
cursor: col-resize;
}
.glass {
height: 100%;
width: 100%;
cursor: col-resize;
display: none;
position: absolute;
}
.grandchild {
background-color: red;
width: 50px;
height: 50px;
position: absolute;
top: 0;
left: 0;
}Run Code Online (Sandbox Code Playgroud)
<div id="container" class="container">
<div id="glass" class="glass"></div>
<div class="c1">
<div class="grandchild"></div>
</div>
<div id="c2" class="c2"></div>
</div>Run Code Online (Sandbox Code Playgroud)
<div class="grandchild">
<div class="content"></div>
</div>
Run Code Online (Sandbox Code Playgroud)