Last active
March 25, 2019 00:04
-
-
Save ilearnjavascript/f1faf3280473e7b5a3830e1c661eb846 to your computer and use it in GitHub Desktop.
fadeout-plainjs.js
This file contains 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
const fadeOut = (el, smooth = true, displayStyle = 'none') => { | |
if (smooth) { | |
let opacity = el.style.opacity; | |
let request; | |
const animation = () => { | |
el.style.opacity = opacity -= 0.04; | |
if (opacity <= 0) { | |
opacity = 0; | |
el.style.display = displayStyle; | |
cancelAnimationFrame(request); | |
} | |
}; | |
const rAf = () => { | |
request = requestAnimationFrame(rAf); | |
animation(); | |
}; | |
rAf(); | |
} else { | |
el.style.opacity = 0; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment