JSFiddle - React, Tailwind, and code Playground

HTML

<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>

CSS

div {
    width: 50px;
    height: 50px;
    position: absolute;
    top: 0;
    left: 0;
}

#div1 {
    z-index: 4;
    background-color: Red;
}
#div2 {
    z-index: 3;
    background-color: Blue;
}
#div3 {
    z-index: 2;
    background-color: Yellow;
}
#div4 {
    z-index: 1;
    background-color: Green;
}

JavaScript

$(function() {
    // the divs that we want to cycle through are named here.
    var divs = ["#div1", "#div2", "#div3", "#div4"];

    function doFade(items, index) {
        $(items[index]).fadeTo(1000, 0, doFade.bind(this, items, index + 1));
    }

    doFade(divs, 0);
});