DEV Community

Bruno Vieira
Bruno Vieira

Posted on • Updated on

Vanilla JS FadeIn/Out

It looks like JQuery is slowly starting to be "a thing of the past".. so i recently migrated my project from JQuery to Vanilla.. and the result is quite satisfying.

First, you can be sure that if you made it with JQuery, you will make it with Vanilla aswell, but keep in mind that may required a few more lines of code.

One of the things that i used to use alot with JQuery was FadeIn/FadeOut... now the question was: how to do it in Vanilla?

Well... quite easy actually.
Hope it helps someone.

        // ** FADE OUT FUNCTION **
        function fadeOut(el) {
   = 1;
            (function fade() {
                if (( -= .1) < 0) {
           = "none";
                } else {

        // ** FADE IN FUNCTION **
        function fadeIn(el, display) {
   = 0;
   = display || "block";
            (function fade() {
                var val = parseFloat(;
                if (!((val += .1) > 1)) {
           = val;
Enter fullscreen mode Exit fullscreen mode

It works really well and it gives your project a little personality :P

Top comments (3)

realto619 profile image
Paul Leech

Without a time component, the effect is basically too brief to notice.
Check out this solution for the fade in effect:

eterychan profile image

Rather than adding timer or interval, you can simply reduce the value 0.1 to 0.05 to make the animation slower. I have tested it myself.

krasimir1 profile image

Hello! This didnt work for me quite well.
When i changed this if (!((val += .1) > 1)) { to this if (!((val += .1) >= 1)) { it started working well.