So the job was to animate an element when they get on the page. The people on the internet had the same issue so they made the Intersection Observer API. Thanks!
Lets check it out. Just scroll through this page and check out the fading effect on the cards. And then scroll down to the explanation.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut beatae cupiditate eaque et harum impedit ipsam laboriosam maxime nesciunt obcaecati, sint velit! Dignissimos officiis reiciendis repellat temporibus unde voluptatem voluptatum.
It's quite simple, the intersection observer has some calls to check if an item intersects with the element you want. On this example i will observe on the window. So no argument is needed. Lets just code.
Here we use the isIntersecting if the element is intersecting, with the window. If it is we add a class in-view to the intersecting elements. For the observer we use a class js-observe. And to that nodelist we add let the IntersectionObserver observe. When an elements gets on screen the element now gets a in-view class, wich we will animate. And that's it. So simple. Now for some animation, css to the rescue.
Just note that the intersection observer is not supported jet in all browsers, check that on caniuse. But there is an IntersectionObserver polyfill which supports major browsers.
I did add a small delay on the animation when the elements get on the screen. But other than that just add it and enjoy the animations.