Forem

Bruno Vieira
Bruno Vieira

Posted on • Edited on

15 3

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) {
            el.style.opacity = 1;
            (function fade() {
                if ((el.style.opacity -= .1) < 0) {
                    el.style.display = "none";
                } else {
                    requestAnimationFrame(fade);
                }
            })();
        };

        // ** FADE IN FUNCTION **
        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);
                }
            })();
        };
Enter fullscreen mode Exit fullscreen mode

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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (3)

Collapse
 
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: jsfiddle.net/TH2dn/606/

Collapse
 
eterychan profile image
Eternia

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.

Collapse
 
krasimir1 profile image
Krasimir1

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.

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay