JSFiddle - React, Tailwind, and code Playground

by jfriend00

HTML

<button onclick="fadeUp(document.getElementById('test'), 2000)">Fade</button>
<div id="test"></div>

CSS

#test {
    height: 200px;
    width: 400px;
    background-color: red;
    opacity: 0;
}

JavaScript

function fadeUp( element, max_time ) {
    var elapsed = 0;
    
    function next() {
        elapsed += 10;
        element.style.opacity = elapsed / max_time;
        if (elapsed <= max_time) {
            setTimeout(next, 10);
        }
    }
    next();
}