-
-
Save realto619/538e8c71779cde7c68ba7acc1759a926 to your computer and use it in GitHub Desktop.
fadeIn & fadeOut in vanilla js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
})(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made 2 minor adjustments to extend the duration of the effect.