Animate and slide in HTML elements from any direction in CSS and Javascript

Blog Problem Solving Tutorial

How to animate HTML elements and slide them in on mouse scroll using CSS and Javascript

How to animate HTML elements and slide them in on mouse scroll using CSS and Javascript

Today I want to animate a bunch of HTML elements and create an animation that moves them inside the same parent DIV once the elements are in the viewport, so only when the elements are visible to the users.

To do this, first of all, I need some CSS classes, one that sets the elements’ opacity to zero (this class is called “‘js-animate-element“), so the elements are not going to be visible at first, then 4 different CSS classes that move the elements to the initial positions, which could be top/bottom/left/right, these classes are called “bottom-animation, top-animation etc…”, and another CSS class that move the elements to the initial position, which is called “insight“. In order to set the initial element position I will use the CSS attribute “transform: translateY(—px);

Here is the CSS code that I need:

.js-animate-element {
    opacity: 0;

    &.bottom-animation{
        transform: translateY(150px);
    }

    &.top-animation{
        transform: translateY(-150px);
    }

    &.right-animation{
        transform: translateX(150px);
    }

    &.left-animation{
        transform: translateX(-150px);
    }

    &.insight {
        transform: translateY(0) translateX(0);
        opacity: 1;
        transition: 1s all ease;
    }
}

Now I want to put my HTML elements inside the same parent that I will call “js-animate-wrapper“. When the user scrolls the page I will constantly check if one of the parent elements called “js-animate-wrapper” is in the viewport or if it enters in viewport. So I will use the Javascript listener event called “window.addEventListener(“scroll”, function)“, here is the documentation.

If one of the parents is in the viewport, I will check if the parent has children elements called “js-animate-element”, if so, I will loop through them with a forEach loop and I will apply the CSS class “insight” to them in order to bring the elements back to the initial position. But because I want to do that with one single element at a time, I will run a “setTimeout” function inside the forEach loop that will wait 500 milliseconds before running the next element.

And here is the JS code of the tutorial:

(function () {

    function reveal() {
        var wrapper = document.querySelectorAll(".js-animate-wrapper");

        wrapper.forEach(function (parent, index) {
            var windowHeight = window.innerHeight;
            var elementTop = parent.getBoundingClientRect().top;
            var elementVisible = 150;

            if (elementTop < windowHeight - elementVisible) {
                var reveals = parent.querySelectorAll(".js-animate-element");

                reveals.forEach(function (ele, index) {
                    setTimeout(function () {
                        ele.classList.add("insight");
                    }, index * 500);
                });
            }
        });
    }

    window.addEventListener("scroll", reveal);
    reveal();

})(jQuery);

I have created this codePen script with the whole tutorial in it and a funny demo to watch, please have a look at this link in codePen. And if you have enjoyed this tutorial leave a comment below and tell me what you think about it.

Click to Leave a Comment

You May Like

Ever walked into a store, saw something you liked, and thought… hmm, maybe I can ask for a better price? It happens online too. People want choices. They want wiggle room. Some want bulk deals. Others want special setups. A few just want to talk before they spend money. And when a WooCommerce store doesn’t […]

December 14, 2025
Read More...

How did we get here so fast? Crazy. Just a few decades ago, the internet was dull. Plain. Silent. No colors. No animations. Just black text on white screens. Websites didn’t move, didn’t react. They just sat there. Developers typed lines of HTML like magicians writing spells. Simple pages came alive—barely. You could click, scroll, […]

November 11, 2025
Read More...

Why make shoppers wait?Ever waited for a page to reload after clicking “Add to Cart”? Annoying, right? The screen freezes. The spinning wheel mocks you. And for a second, you think, maybe I don’t even need this thing. That’s how your customers feel, too. Shopping online is supposed to be fast. Smooth. Effortless. But clunky […]

October 1, 2025
Read More...