Skip to content

Instantly share code, notes, and snippets.

@realto619
Forked from alirezas/fade.js
Last active September 17, 2022 00:00
Show Gist options
  • Save realto619/538e8c71779cde7c68ba7acc1759a926 to your computer and use it in GitHub Desktop.
Save realto619/538e8c71779cde7c68ba7acc1759a926 to your computer and use it in GitHub Desktop.
fadeIn & fadeOut in vanilla js
function fadeOut(_target){
let el = document.querySelector(_target);
el.style.opacity = 1;
(function fade() {
if ((el.style.opacity -= 0.02) < 0) {
el.style.display = "none";
} else {
requestAnimationFrame(fade);
}
})();
};
function fadeIn(_target, display){
let el = document.querySelector(_target);
el.style.opacity = 0;
el.style.display = display || "block";
(function fade() {
var val = parseFloat(el.style.opacity);
if (!((val += 0.02) > 1)) {
el.style.opacity = val;
requestAnimationFrame(fade);
}
})();
};
@realto619
Copy link
Author

I made 2 minor adjustments to extend the duration of the effect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment