He gets stuck in the amount of children
This is the expected behavior when using flexbox . I think if you want to scroll to the end, you can use position: absolute for grandchild relative to c1 :
.grandchild { background-color: red; width: 50px; height: 50px; position: absolute; top: 0; left: 0; }
Give overflow: hidden in c1 too:
.c1 { background-color: blue; flex: 1; height: 100%; position: relative; overflow: hidden; }
Hooray!
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);
.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; }
<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>
Decision
Therefore, I assume that your strategy should be to use an absolute grandchild that fills the entire sidebar and then puts the content inside, like:
<div class="grandchild"> <div class="content"></div> </div>
and change these styles:
.grandchild { position: absolute; top: 0; left: 0; right: 0; bottom: 0; } .grandchild .content{ background-color: red; width: 50px; height: 50px; }
Example below:
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);
.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 { position: absolute; top: 0; left: 0; right: 0; bottom: 0; } .grandchild .content{ background-color: red; width: 50px; height: 50px; }
<div id="container" class="container"> <div id="glass" class="glass"></div> <div class="c1"> <div class="grandchild"> <div class="content"></div> </div> </div> <div id="c2" class="c2"></div> </div>
source share