Last active
July 21, 2020 15:08
-
-
Save upalexgill/9871eb12aa820459fdc2fa8acf3b25da to your computer and use it in GitHub Desktop.
Fade in/out
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 fadeIn(el, duration, callback) { | |
| el.style.opacity = 0 | |
| let last = +new Date() | |
| function fade () { | |
| el.style.opacity = +el.style.opacity + (+new Date() - last) / duration | |
| last = +new Date() | |
| if (+el.style.opacity < 1) { | |
| (window.requestAnimationFrame && window.requestAnimationFrame(fade)) || setTimeout(fade, 250) | |
| } else { | |
| if (typeof callback === 'function') callback() | |
| } | |
| } | |
| fade() | |
| } | |
| function fadeOut(el, duration) { | |
| el.style.opacity = 1 | |
| let last = +new Date() | |
| function fade () { | |
| el.style.opacity = +el.style.opacity - (+new Date() - last) / duration | |
| last = +new Date() | |
| if (+el.style.opacity > 0) { | |
| (window.requestAnimationFrame && window.requestAnimationFrame(fade)) || setTimeout(fade, 250) | |
| } else { | |
| el.style.display = 'none' | |
| } | |
| } | |
| fade() | |
| } | |
| // const el = document.querySelector('.fade') | |
| // fadeIn(el, 3000) | |
| // fadeOut(el, 3000) | |
| // fadeIn(el, 3000, () => fadeOut(el, 3000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment