Skip to content

Instantly share code, notes, and snippets.

@h1ldebrand
Created October 16, 2017 12:53
Show Gist options
  • Save h1ldebrand/6ac9d5e9579fb24a68c762aeb4826e8e to your computer and use it in GitHub Desktop.
Save h1ldebrand/6ac9d5e9579fb24a68c762aeb4826e8e to your computer and use it in GitHub Desktop.
my own function fadeOut on Vanilla.js
function fadeOut(selector, time){
let timer = time || 400;
let element = document.querySelectorAll(selector)[0];
let timeForSetInterver = 20;
let opacityStart = getComputedStyle(element).opacity;
let count = timer / timeForSetInterver;
let step = opacityStart / count;
let i = 0;
let forClear = setInterval(function(){
let opacity = getComputedStyle(element).opacity;
element.style.opacity = parseFloat(opacity) - parseFloat(step);
i++;
if(i == count){
clearInterval(forClear);
element.style.opacity = 0;
}
}, timeForSetInterver)
}
@AlejandroJSR7
Copy link

Thank you very much, I was looking for something like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment