JSFiddle - React, Tailwind, and code Playground
HTML
<div id="viewport"></div>
CSS
#viewport {
position: relative;
width: 400px;
height: 300px;
margin: 100px auto;
border: 1px solid #333333;
overflow: hidden;
}
#viewport .smoke {
position: absolute;
width: 20px;
height: 80px;
background: rgba(0, 0, 0, 0.5);
bottom: 0px;
}
JavaScript
(function () {
"use strict";
var i = 0;
for (; i < 5; i += 1) {
setTimeout(function addSmoke() {
var
time = Math.random() * 1000 + 3000,
smoke = $('<div />', {
class: 'smoke',
css: {
opacity: 0,
left: Math.random() * 175 + 50
}
});
// add to viewport
$(smoke).appendTo('#viewport');
// animate
$.when(
// animate to 100% opacity in half the time (fade in)
$(smoke).animate({
opacity: 1
}, {
duration: time / 2,
easing: 'linear',
queue: false,
// animate to 0% opacity in the remaining time (fade out)
complete: function () {
$(smoke).animate({
opacity: 0
}, {
duration: time / 2,
easing: 'linear',
queue: false
});
}
}),
// animate movement
$(smoke).animate({
bottom: $('#viewport').height()
}, {
duration: time,
easing: 'linear',
queue: false
})
// when all down, remove and add new random smoke
).then(function () {
$(smoke).remove();
addSmoke();
});
}, Math.random() * 10000);
}
}());