I have made recently one answer to another user about a generic method for JQuery::slideUp() sequentially multiple items. If you want a reference, you can check it here:
slide up elements in a sequential order
After come to a solution the OP
asked me about an initial jumping effect that you can observe on the next code:
$('button').on('click', function()
{
close_all($(".block"));
});
function close_all(elements, idx=0)
{
if (idx >= elements.length)
return;
let cb = () => close_all(elements, idx + 1);
elements.eq(idx).slideUp(2000, cb);
}
.block {
display: inline-block;
width:14%;
background:gold;
margin:0 9px;
}
#block1{
height:54px;
}
#block2{
height:25px;
}
#block3{
height:72px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='block' id='block1'>BLOCK 1</div>
<div class='block' id='block2'>BLOCK 2</div>
<div class='block' id='block3'>BLOCK 3</div>
<button>CLICK</button>
After digging a little about it, I have come that this is related to display:inline-block
property he has over the .block
elements. For what I have read so far (I'm not experimented on CSS
) this property it a sort of replacement for float:left
. So I decided to change the style to use float:left
instead of display:inline-block
and the issue disappear. You can check it on next code:
$('button').on('click', function()
{
close_all($(".block"));
});
function close_all(elements, idx=0)
{
if (idx >= elements.length)
return;
let cb = () => close_all(elements, idx + 1);
elements.eq(idx).slideUp(2000, cb);
}
.block {
float: left;
width:14%;
background:gold;
margin:0 9px;
}
#block1{
height:54px;
}
#block2{
height:25px;
}
#block3{
height:72px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='block' id='block1'>BLOCK 1</div>
<div class='block' id='block2'>BLOCK 2</div>
<div class='block' id='block3'>BLOCK 3</div>
<button>CLICK</button>
Anyone has idea why the issue appears when we use display:inline-block
?