observe()
Method
The observe()
method is the most important method provided by the ScrollObserver()
class. It's use to observe elements position on the page and add an animation when the element being observed enter or leave the viewport.
Here's a basic syntax on how to use it to observe elements and trigger animations on entering viewport:
observer.observe(elements, callback, cssAnimationClass);
The observer
here is the variable we created earlier when we create a new instance of the ScrollObserver
class. You can call it anything you want.
observer() paramsβ
Here's a breakdown of the observer()
method parameters:
1. elementβ
- Type:
element/elements
- Optional:
false
- Description: These are the html
elements
to observe and animate when they enter the viewport. Can be a single or multiple elements.
- Single Element
- Multiple Elements
observer.observe(document.querySelector('.my-card'), null, cssAnimationClass);
observer.observe(document.querySelectorAll('.my-cards'), null, cssAnimationClass);
You can also get your elements like this:
- Single Element
- Multiple Elements
const myCard = document.querySelector('.my-card');
observer.observe(myCard, null, cssAnimationClass);
const myCards = document.querySelectorAll('.my-cards');
observer.observe(myCards, null, cssAnimationClass);
2. callback function (optional)β
- Type:
callback function
- Optional:
true
- Description: An optional callback function to be called when currenlty observed elements enter the viewport. Replace with
null
when you don't need a callback function
- With Callback Function
- Without Callback Function
observer.observe(elements, (el, index) => console.log(`hello from element at ${index}`), cssAnimationClass);
// Logs 'hello from element at index of the element' to the console for each element that enters the viewport
observer.observe(elements, null, cssAnimationClass);
The second parameter i.e index
is optional and can be totally omitted when using the callback function.
Sounds cool and all, but do I really need this? π€ Yes, you absolutely do! π
You can get super creative with the callback function β itβs not just for triggering animations. For example, hereβs how you can lazy load images effortlessly: πΈπ‘
const images = document.querySelectorAll('.my-imgs[data-src]');
observer.observe(images, (img) => {img.src = img.dataset.src}, cssAnimationClass);
Boom β smooth and optimized loading without extra libraries. Minimal code, maximum impact β‘
Use it your way. Get playful. Get productive!
3. Css animation classβ
- Type:
string
- Optional:
false
- Description: This the css class that will be applied to elements you are observing once they enter the viewport
Here's how you can animate elements by applying new css class to them once they are visible in the viewport:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Scroll Observer Demo </title>
<Link rel="stylesheet" href="./style.css" />
</head>
<body>
<div class="base-class my-card"></div>
<div class="base-class my-card"></div>
<div class="base-class my-card"></div>
<div class="base-class my-card"></div>
<div class="base-class my-card"></div>
<script src="https://cdn.jsdelivr.net/npm/@charmingdc/scrolljs@2.0.0/dist/scroll.esm.min.js" type="module"></script>
<script src="./script.js" type="module"></script>
</body>
</html>
.base-class {
opacity: 0;
transform: translateY(1rem);
transition: 0.6s ease;
}
.animate-class {
opacity: 1;
transform: translateY(0);
}
const observer = new ScrollObserver();
observer.observe(document.querySelectorAll('.my-card'), (el) => console.log(`I'm visible`), 'animate-class');
Omit the leading dot (.) when passing the css animation class
And just like that β boom!
Your elements are now being observed and animated as soon as they enter the viewport! β¨
Donβt feel like writing custom CSS animations? Perfect. Built-in animation classes are now supported in v2.0.0
! π
Just apply them β no extra setup needed.
Less hassle, more motion. β‘