Created
March 24, 2019 21:32
-
-
Save ilearnjavascript/ac50beeadaf1dafc53c1a0e12154e671 to your computer and use it in GitHub Desktop.
fadein-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 fadeIn = (el, smooth = true, displayStyle = 'block') => { | |
el.style.opacity = 0; | |
el.style.display = displayStyle; | |
if (smooth) { | |
let opacity = 0; | |
let request; | |
const animation = () => { | |
el.style.opacity = opacity += 0.04; | |
if (opacity >= 1) { | |
opacity = 1; | |
cancelAnimationFrame(request); | |
} | |
}; | |
const rAf = () => { | |
request = requestAnimationFrame(rAf); | |
animation(); | |
}; | |
rAf(); | |
} else { | |
el.style.opacity = 1; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment