ScrollJs
Easy scroll triggered animation library
ScrollJs is a lightweight and easy to use javascript library that allows you to add scroll triggered animations (AOS) with optional callback functions and also lazy load your images to make your website more optimize.
While other scroll triggered animation library offer predefined animation classes not giving full control and how you want your animations to display, ScrollJs allows you to use your already styled animation class.
Installation
- Install npm package
Install directly from npm registry as a dev dependency
- Use via cdn link
Paste the code below in between the head tag in your html code
Initialization
- For Es Module
- For CommonJs
If you used the cdn version, simply add the following javascript code to Initialize scrollJs
- Optional parameters
-
animateOnce (boolean) : This is the first parameter that ScrollObserver() can take in. By default it is set to false, i.e elements that are being observe should always animate whenever they enter the viewport, if set to true, elements being observe will only animate the first time they enter the viewport. To set the parameter:
- JSconst observer = new ScrollObserver(boolean) // here replace "boolean" with either true or false -
observer options (object) : This is the second parameter you can pass in when you Initialize ScrollObserver(). It takes in your custom settings for the .observe() method as key-value pairs allowing you to set custom root, threshold, and rootMargin. To set this parameter, the first parameter must be set i.e the animateOnce (boolean).
- JSconst rootDiv = document.querySelector('.your-container'); const observer = new ScrollObserver(false, { root: rootDiv, threshold: 0.8, rootMargin: '-10px', }); //Note: all observer options are optional // root: to set a use a custom container height as the viewport for the animations to trigger. set value to "null" to use default viewport //threshold specifies how much of the body the elements you are observing should be visible for the animation to trigger. //rootMargin to shrink your viewport height.The table below shows the "observer options" name, type, default value, minimum and maximum values.
NAME TYPE DEFAULT MIN MAX root element brower viewport ~ ~ threshold number 0.8 0 0.8 rootMargin string -10px -80px 80px
Note
When threshold is set up to 0.8, rootMargin must be between the range of '-10px' to '10px' and when rootMargin is upto '-70px' or '70px', threshold must not be more than 0.2 else your elements may not animate/show on scroll.
Methods
ScrollJs has three main methods, .observe(), .unobserve(), .disconnect()
.observe();
This method is use to observe elements position on the page and add an animation when the element being observed enter or leave the viewport.
The table below shows the parameters name, type and whether it is neccesary.
NAME | TYPE | OPTIONAL |
---|---|---|
element | element | No |
callback | function | Yes |
animationClass | string | No |
- The .observe() method can be called multiple times, to observe elements with different clases/id's and add different animations to trigger when the elements enter the viewport.
- The first parameter (element) can also take in multiple elements with the same class name
- Lazy loading images with .observe(); method
By setting a function for the callback parameter you can lazy load images resulting in more optimize websites.
Note
set callback to null if no callback function is set, e.g observer.observe(element, null, 'show');
.unobserve();
.unobserve() method only accept one parameter which is/are the element/elments to stop observing
The .unobserve() method is useful when you want to unobserve a single element among different elements with the same class name that is currently being observe.
use case:
.disconnect();
The .disconnect() method stops ScrollObserver from tracking all currently observed elements. Once this method is called, the observer will no longer trigger any callbacks or apply animations for the elements it was previously observing.
After calling .disconnect(), the observer is effectively reset and no longer tracks any elements. To resume observing elements, you must explicitly call the .observe() method again and pass the elements you wish to track
Use case:
All the .observe() method won't observe the elements unless they are explicitly re-observed after the disconnect() method
Demo
Demo usage of all the methods for you to experiment with.
If the result of any of the CodePens didn't show up, simply tap the Rerun button.
.observe() method
Here is a demo of using the .observe() method with the default parameters
Another demo using the .observe() method, but setting the animateOnce parameter to true when initializating ScrollObserver()
.unobserve() method
A demo usage of the .unobserve() method, here the fifth card won't display. Check the javascript tab for more info.
.disconnect() method
The .disconnect() method simply clear all current observers and make them stop observing any elements that are being observed before.
Simply call the .disconnect() method without any parameter to stop all observers from observing the current elements they are observing.
ScrollJs in Action
Here are some of the websites that uses ScrollJs to add scroll triggered mind blowing animtions to their website
🌊 MindEcho
A mental health companion web app that strives to promote mental wellbeing by offering wide range diverse well-being tools including analysis and insights plus an AI mental Therapist
Visit MindEcho