It appears that the sudden movement you are experiencing is due to the behavior of the flex-grow
property in your flex-box setup. One solution could be to animate the flex-grow
and flex-shrink
properties to create a smoother transition. Please note that this solution has been tested in Chrome only.
To adjust the pseudo min-width, you can modify the width
of the container
class, for example:
.container{
display: inline-flex;
width: 125px;
flex-shrink: 100;
flex-grow: 1;
transition: flex-grow 1s cubic-bezier(0.4, 0, 0.2, 1), flex-shrink 1s cubic-bezier(0.4, 0, 0.2, 1);
}
function toggleClasses(){
document.getElementById('container-1').classList.toggle('active');
document.getElementById('container-2').classList.toggle('active');
}
document.getElementById('container-1').addEventListener('click', () => {
if(document.getElementById('container-1').classList.contains('active')) return;
toggleClasses();
});
document.getElementById('container-2').addEventListener('click', () => {
if(document.getElementById('container-2').classList.contains('active')) return;
toggleClasses();
});
.wrapper{
display:inline-flex;
width: 400px;
border: 1px dotted black;
box-sizing: border-box;
overflow: auto;
}
.wrapper .container:nth-child(1){
background: lightblue;
}
.wrapper .container:nth-child(2){
background: blue;
}
.wrapper .container:nth-child(1) div:last-child{
background: rgba(255,0,0,0.33);
cursor: pointer;
}
.wrapper .container:nth-child(2) div:last-child{
background: rgba(0,255,0,0.33);
cursor: pointer;
}
.container{
display: inline-flex;
width: 100%;
flex-shrink: 100;
flex-grow: 1;
transition: flex-grow 1s cubic-bezier(0.4, 0, 0.2, 1), flex-shrink 1s cubic-bezier(0.4, 0, 0.2, 1);
}
.container.active{
flex-grow: 100;
flex-shrink: 1;
}
.element{
width: 100%;
}
<div class="wrapper">
<div id="container-1" class="container active">
<div>1</div>
<div class="element">Content#1</div>
</div>
<div id="container-2" class="container">
<div>1</div>
<div class="element">Content#2</div>
</div>
</div>