It gives you full control over animation styles and works with existing CSS classes or third-party animation libraries like Animate.css.
ScrollJs works by using the Intersection Observer API to detect when elements enter or leave the viewport. This implementation allows for flexible, performant scroll-based animations and actions, with options for one-time or repeated triggers as elements enter and leave the viewport.
1. Install and import ScrollJs into your project.
# NPM $ npm install @charmingdc/scrolljs
// ES
import ScrollObserver from '@charmingdc/scrolljs'
// CommonJs
const ScrollObserver = require ('@charmingdc/scrolljs') 2. Or insert the ScrollJs JavaScript library in your HTML document:
<script src="/path/to/scroll.min. js"></script>
3. Initialize ScrollObserver. The animateOnce parameter determines if animations trigger once or repeatedly. The options object allows customization of the Intersection Observer API settings.
const observer = new ScrollObserver(false, {
root: rootDiv,
threshold: 0.8,
rootMargin: '-10px',
}); 4. Use the .observe() method to trigger animations when the target element is scrolled into view.
observer.observe(myElement, null, 'myAnimation');
.myAnimation {
/* Your CSS animation properties */} 5. You can also use .observe() with a callback function to lazy load images.
observer.observe(image, (img) => {
img.src = img.dataset.src;}, 'visible'); This sets the image src attribute when the image becomes visible, using a placeholder data-src attribute in your HTML.
6. Unobserve & Disconnect:
// Stops observing a specific element. observer.unobserve(element): // Stops observing all elements. observer.disconnect():
05/03/2025
The post ScrollJs: Custom Scroll Animations and Lazy Loading appeared first on CSS Script.
Amazon is going through something of a massive restocking mission this week for Pokémon cards,…
Amazon is going through something of a massive restocking mission this week for Pokémon cards,…
Magic: The Gathering has kicked off its Teenage Mutant Ninja Turtles set prerelease weekend, but…
The much-delayed Spider-Man: Beyond the Spider-Verse currently has a June 18, 2027 release date. If…
Resident Evil Requiem has landed on Steam, and is now the series' biggest launch to…
There's a whole bunch of great Pokémon deals available right now online as part of…
This website uses cookies.