Skip to content

Instantly share code, notes, and snippets.

@upalexgill
Last active July 21, 2020 15:08
Show Gist options
  • Select an option

  • Save upalexgill/9871eb12aa820459fdc2fa8acf3b25da to your computer and use it in GitHub Desktop.

Select an option

Save upalexgill/9871eb12aa820459fdc2fa8acf3b25da to your computer and use it in GitHub Desktop.
Fade in/out
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