🎯 Scroll Animation using Intersection Observer API
✨ Animating elements onto the screen looks fascinating. There are quite a few libraries to do that but what if you don't want to do it that way? Then you can use JavaScript Intersection Observer API, what it does is tell you if an element in the DOM is visible to the viewport. If an element is intersecting you can apply a CSS class that animates into the DOM.
The approach, is to create the intersection observer, a Class that takes a callback in its Constructor. The cool about this thing is it can observe multiple entries at the same time. This function will run anytime the visibility of one of the observed elements changes because it handles multiple entries. So we need to loop over and run a conditional check to find out if that entry is intersecting the viewport or not. If it's intersecting we'll add animation class/es to it which will make it visible and if we want to show the animation over and over again we need to remove the class/es when it's not intersecting. That's our observer setup.
Now we need to tell the observer what to observe. In this demo, we simply loop over all the [data-animation] elements and pass them to the observer to observe each one of them. That's all the JavaScript code we need. The rest is done by CSS animation.
Have Fun, Cheers! 🍻
🕹 See it in action: https://codepen.io/iamsaief/full/qBYPdGx
💬 Let me know your thoughts about it or share ideas.
#scrollAnimation #intersectionObserverAPI
#csstricks #jstricks #tipsandtricks #html #css #sass #scss #javascript #reactjs #frontend #webdeveloper #frontenddeveloper #javascriptdeveloper
Auf dieser Seite können Sie das Online-Video Scroll Animation using Intersection Observer API mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Saief Al Emon 22 September 2023 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 45 Mal angesehen und es wurde von 0 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!