JSFiddle - React, Tailwind, and code Playground
HTML
<button id="click1">stop</button>
<button id="click2">start</button>
<div id="collection_divs">
<div class="div">1</div>
<div class="div">2</div>
<div class="div">3</div>
</div>
CSS
.div {
width: 150px;
height: 30px;
background-color: red;
float: left;
margin-right: 2px;
margin-bottom:10px;
margin-top:10px;
}
button {
width: 100px;
height: 40px;
}
JavaScript
(function(){
var elem, startInterval, removedElem, widthBlock,
btn1 = document.getElementById('click1'),
btn2 = document.getElementById('click2'),
collectionBlocks = document.getElementById('collection_divs');
function clearThisInterval(){
if (thisElem.clientWidth == 0) {
removedElem = collectionBlocks.removeChild(collectionBlocks.children[0]);
collectionBlocks.appendChild(removedElem);
thisElem.style.width = '150px';
clearInterval(startInterval);//удаляем интервал
// для следующего блока
}
}
function minWidth(){
thisElem = collectionBlocks.children[0];
widthBlock = thisElem.clientWidth;
// Уменьшаем ширину
if (widthBlock != 0) {
startInterval = setInterval(function(){
thisElem.style.width = [widthBlock -= 2] + 'px';
}, 30);
}
setTimeout(clearThisInterval, 2253); // 15 строка //удаляем интервал
}
// Запускаем интервал / строка 19
btn2.onclick = function(){setInterval(minWidth, 3000); }
//становить или пауза интервала для уменьшение ширины / 23 строка //
btn1.onclick = function() { clearInterval(startInterval); }
//при загрузке стартует сам
//setInterval(minWidth, 5000);
})();