Last active
October 10, 2019 20:27
-
-
Save tobiasroeder/54d5dfc02dbc7bb3a6fb1b42fafa93a2 to your computer and use it in GitHub Desktop.
Simple fade in/out function without any library. (pur JavaScript)
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
// source: https://chrisbuttery.com/articles/fade-in-fade-out-with-javascript/ | |
// fadeOut (fade.js) | |
function fadeOut(el) { | |
el.style.opacity = 1; | |
(function fade() { | |
var val = parseFloat(el.style.opacity); | |
if (!((val -= .1) < 0)) { | |
el.style.opacity = val; | |
requestAnimationFrame(fade); | |
} else { | |
el.style.display = "none"; | |
} | |
})(); | |
return true; | |
} | |
// fadeIn (fade.js) | |
function fadeIn(el, display) { | |
el.style.opacity = 0; | |
el.style.display = display || "block"; | |
(function fade() { | |
var val = parseFloat(el.style.opacity); | |
if (!((val += .1) > 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