How can we efficiently and easily detect when elements on a web page become visible or hidden, without slowing down the page or writing complex code to handle scroll events and visibility calculations, especially in cases like loading images only when needed (lazy-loading), creating infinite scrolls, or triggering animations when content appears?
As developers, we often want to load content like images or new articles, only when it's visible on the user's screen to save bandwidth and improve page loading times. However, this can be tricky because there is a need to know exactly when these elements come into view as the user scrolls on the web page.
Traditionally, this requires setting up scroll event listeners and manually calculating element positions, which could lead to slow and clunky performance, especially on pages with lots of content or on devices with limited processing power.
// Function to fetch data from an API function fetchData() { console.log('Fetching more data...'); // Simulate an API request using fetch fetch('https://jsonplaceholder.typicode.com/posts?_limit=5&_page=1') .then(response => response.json()) .then(data => { // Display the fetched data on the page }) .catch(error => console.error('Error fetching data:', error)); } // Function to check if the user is near the bottom of the page function checkScrollPosition() { const scrollPosition = window.innerHeight + window.scrollY; const threshold = document.body.offsetHeight - 100; if (scrollPosition >= threshold) { fetchData(); } } // Add the scroll event listener to the window object window.addEventListener('scroll', checkScrollPosition); // Initial call to load some data when the page first loads fetchData();
The event listener added might look simple but, you will notice the issues that come with it when you are working on a page with lots of data. The event Listener is designed to fire multiple times in a second to achieve listening for events, while this works it affects page performance as JavaScript is a single-threaded programming language. Also to achieve the same effect that the intersection observer can do, the developer needs to take note of viewport dimensions and add calculations that will be done regularly still affecting the performance.
The challenge is to detect visibility changes without writing complex code or making the page lag. Solving this requires a more efficient way to monitor visibility that doesn’t slow down the browser with constant checks and calculations. This is where the Intersection Observer API comes in, offering a simpler, more performance-friendly solution to these visibility detection challenges.
The Intersection Observer API is a powerful tool in web development that solves this challenge. This tool simplifies detection when elements on a webpage enter or exit the visible area (windows viewport) without complex calculations.
Instead of constant scroll event checks, which can affect the performance of JavaScript's single CPU thread feature, the API lets the browser automatically handle visibility detection, allowing for smoother implementation of features like lazy loading images, infinite scrolling, animations triggered by element visibility and reporting the visibility of web ads to calculate ad revenue. By offloading these tasks to the browser, the Intersection Observer API helps keep pages fast and efficient, especially on complex or content-heavy sites. Additionally, the Intersection Observer API is compatible with all browsers, making it an adept solution for tracking scroll events without any trade-offs.
By the end of this article, you'll:
This post will show you how to use the Intersection Observer API and JavaScript's fetch() function to get data and update your page with it. We'll go through the steps to build a web experience that’s both efficient and engaging. Let's get started!
The Intersection Observer typically keeps track of when a particular piece appears on the screen. It determines if the element fills the viewport or the scrollable portion of the screen. You must set the observer's root option to null to see changes in visibility concerning the entire screen. This helps keep track of items when they scroll into view.
Although the word "root" may sound unfamiliar, you'll understand why as you read on?.
The API functions in the same way regardless of whether you use the viewport or another element as the root; it calls a callback function that you supply each time the target element's visibility varies to the point where it passes the appropriate degree of intersection with the root.
The degree of intersection between the target element and its root is the intersection ratio. This is a representation of the percentage of the target element which is visible as a value between 0.0 and 1.0.
To create the intersection observer, you'll call its constructor and pass an option object for behaviour and a callback function to be invoked whenever a threshold is crossed in one direction or the other:
let options = { root: document.querySelector("#ObservableArea"), rootMargin: "0px", threshold: 1.0, }; let observer = new IntersectionObserver(callback, options); //A threshold of 1.0 means that when 100% of the target is visible within the element specified by the root option, the callback is invoked.
The options object passed into the IntersectionObserver() constructor lets you control the trigger and behaviour of the observer's callback to be invoked. The Options object contains the following keys:
root:
The element that is used as the viewport for checking the visibility of the target. It must be a scrollable container or ancestor of the target. Intersection Observer defaults to the browser viewport if not specified or if null.
rootMargin:
Margin around the root. Can have values similar to the CSS margin property, e.g. "10px 20px 30px 40px" (top, right, bottom, left). The values can be percentages. This set of values serves to grow or shrink each side of the root element's bounding box before computing intersections. Defaults to all zeros.
threshold:
The observer's callback should be executed at a specific % of the target's visibility, indicated by a single or array of numbers. Use a value of 0.5 if you wish to detect only when visibility rises above 50%. To ensure that the callback is executed each time visibility increases by 25%, you can supply the array [0, 0.25, 0.5, 0.75, 1]. The default is 0, which means that the callback will be executed as soon as even a single pixel is displayed. If the value is 1.0, then all pixels must be visible for the threshold to be deemed passed.
After creating the observer, you need a target element to be observed.
let target = document.querySelector("#target"); observer.observe(target); // the callback we set up for the observer will be executed now for the first time // it waits until we assign a target to our observer (even if the target is currently not visible)
Whenever the target meets a threshold specified for the IntersectionObserver, the callback is invoked. The callback receives a list of IntersectionObserverEntry objects and the observer:
let callback = (entries, observer) => { entries.forEach((entry) => { // Each entry describes an intersection change for one observed // target element: // entry.boundingClientRect // entry.intersectionRatio // entry.intersectionRect // entry.isIntersecting // entry.rootBounds // entry.target // entry.time }); };
The list of entries received by the callback includes one entry for each target which reported a change in its intersection status. Check the value of the isIntersecting property to see if the entry represents an element that currently intersects with the root.
N/B!
Be aware that your callback is executed on the main thread. It should operate as quickly as possible; if anything time-consuming needs to be done, use Window.requestIdleCallback().
Also, note that if you specified the root option, the target must be a descendant of the root element.
Now you've got the full gist about intersection observer, I'm excited to get started with the project ?.
In setting up this project, Microsoft Visual Studio Code (VS Code) was used but if you have another text editor or IDE you love, feel free to use that, we won’t judge! ?
However, the steps and procedure to follow are easy, and setting up your project is as easy as taking candy from a baby; that challenge required almost no effort at all. By the end, you'll be able to track when elements appear or disappear on your screen.
You’ll just need three files to get started:
The simplicity of this setup is intentional, as it is designed for beginners. But don't worry—once you’re comfortable, you can use the Intersection Observer in more complex projects utilizing frameworks like React or Vue.
If you're using a Linux or Mac machine copy and paste the code below to your terminal/shell.
#create a directory and enter the mkdir project && cd project #create the files and open VS code touch index.html script.js styles.css #open the current directory on VS Code code .
On Windows, just open VS code on your device, press Ctrl+Shift+N to create a new window, then click on open Folder at the start tab, when the File Explorer opens, create a new folder then click on the new folder and select it. If you don't see the explorer in VS code press Ctrl+Shift+E to open the explorer then click on Create a new file. Give the files any name you want but for the sake of the article I used index.html, style.css and script.js.
After creating your index.html, make sure it looks like the file below
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Movie List with Infinite Scrolling</title> <link rel="stylesheet" href="style.css"> </head> <body> <input type="text" value="" placeholder="enter movie title to search" id="searchInput"> <button id="search">Search</button> <div class="movie-container" id="movieContainer"></div> <div id="loading">Loading...</div> <script src="script.js"></script> </body> </html>
Just follow along with the code below:
body { font-family: Arial, sans-serif; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; flex-direction: column; } .movie-container { width: 80%; max-width: 800px; margin-top: 20px; } .movie-item { display: flex; border: 1px solid #ddd; margin-bottom: 10px; padding: 10px; border-radius: 5px; background-color: #f9f9f9; } .movie-item img { max-width: 100px; margin-right: 20px; } .movie-info { display: flex; flex-direction: column; } .movie-info h3 { margin: 0; margin-bottom: 10px; } .movie-info p { margin: 0; } #loading { text-align: center; margin-top: 20px; display: none; }
const movieContainer = document.getElementById('movieContainer'); const loading = document.getElementById('loading'); const search = document.getElementById('search'); const searchInput = document.getElementById('searchInput'); let currentPage = 1; let observer; const API_KEY = '3676363636'; // Replace with a real API key async function fetchMovies(query, page) { loading.style.display = 'block'; const API_URL = `http://www.omdbapi.com/?s=${query}&apikey=${API_KEY}&page=${page}`; const response = await fetch(API_URL); const {Search} = await response.json(); loading.style.display = 'none'; displayMovies(Search); // Assuming the API returns an array of movies } function displayMovies(movies) { movies.forEach(movie => { const movieItem = document.createElement('div'); movieItem.classList.add('movie-item'); movieItem.innerHTML = ` <img src="${movie.Poster}" alt="${movie.Title}"> <div class="movie-info"> <h3>${movie.Title}</h3> <p><strong>imdbID:</strong> ${movie.imdbID}</p> <p><strong>Year:</strong> ${movie.Year}</p> </div> `; movieContainer.appendChild(movieItem); }); // Reattach the observer to the last movie item if (observer) observer.disconnect(); const lastMovieItem = document.querySelector('.movie-item:last-child'); if (lastMovieItem) observer.observe(lastMovieItem); } // Set up the Intersection Observer observer = new IntersectionObserver(entries => { if (entries[0].isIntersecting) { currentPage++; const query = searchInput.value.trim(); if (query) { fetchMovies(query, currentPage); } } }, { threshold: 1 }); search.addEventListener('click', () => { currentPage = 1; // Reset to the first page on new search movieContainer.innerHTML = ''; // Clear previous results const query = searchInput.value.trim(); if (query) { fetchMovies(query, currentPage); } });
Code Explanation
The index.html is a simple HTML with a head, body, an input to get a query from the user, and a button that the user clicks to fire an action, this action is taken over by the script tag, which calls a function appending the users' query to be fetched by the OMDB API. I used OMDB just for simplicity and illustration, but like I said before, the project structure isn't rigid so readers can choose any method preferred by them.
The style.css contains simple classes for styling the fetched data after being parsed by the script tag. I would not go into details of the styles file as this is not the reason for the article, but developers can always check out w3schools for more tutorials on CSS.
The script.js file is the holy grail of the whole read, for beginners, I'll break the contents of this file into small explainable chunks.
const movieContainer = document.getElementById('movieContainer'); const loading = document.getElementById('loading'); const search = document.getElementById('search'); const searchInput = document.getElementById('searchInput'); let currentPage = 1; let observer; const API_KEY = '3676363636'; // Replace with your real API key
The above code first selects the elements in the index.html and also creates some local variables that will be used in the script.
The currentPage variable is like a page state in the application, that increases as the observer detects an intersection, it is used for querying the OMDB API. The observer variable is defined using the let keyword to be assigned to the IntersectionObserver() constructor function in the later part of the script.
async function fetchMovies(query, page) { loading.style.display = 'block'; const API_URL = `http://www.omdbapi.com/?s=${query}&apikey=${API_KEY}&page=${page}`; const response = await fetch(API_URL); const {Search} = await response.json(); loading.style.display = 'none'; displayMovies(Search); // Assuming the API returns an array of movies } function displayMovies(movies) { movies.forEach(movie => { const movieItem = document.createElement('div'); movieItem.classList.add('movie-item'); movieItem.innerHTML = ` <img src="${movie.Poster}" alt="${movie.Title}"> <div class="movie-info"> <h3>${movie.Title}</h3> <p><strong>imdbID:</strong> ${movie.imdbID}</p> <p><strong>Year:</strong> ${movie.Year}</p> </div> `; movieContainer.appendChild(movieItem); }); //observer code here }
In the code above, the fetchMovies() function contains the query and page parameters, first, the loading element in the HTML is made visible with the display=block, and then API_URL is a variable that holds the preferred API service in string format.
Depending on the API you want to use, some of them offer their services to registered users only. For services like OMDB you have to create an account with them where you'll be given a key upon completion of the registration. That key will be used in the API_URL string. OMDB API sends an array of movies containing 10 items only, the page variable is used to query the API to send the next 10 items after viewing the previous ones. If you noticed I interpolated the string using ${} where I added the query from the user input and also my key from the OMDB movie Service.
const API_URL = `http://www.omdbapi.com/?s=${query}&apikey=${API_KEY}&page=${page}`;
The response variable awaits the response after fetching the API_URL, the data variable parses the response to JSON format using the .json() method on the response variable.
Once the data is fetched the display of the loading element is set to none to hide the loading element.
After that, the displayMovies is called with the destructured Search from the returned JSON. The work of the display function is to accept an array of movies in JSON format and parse each movie to an HTML template containing the movie poster and details of each movie in the array.
// Reattach the observer to the last movie item if (observer) observer.disconnect(); const lastMovieItem = document.querySelector('.movie-item:last-child'); if (lastMovieItem) observer.observe(lastMovieItem);
This code is attached to the displayMovies() function, I'm explaining it separately for adept understanding. In the above code, the observer is disconnected from the last-item queried and attached to the current last-item. This is to ensure the observer is up-to-date at each instance of observing.
// Set up the Intersection Observer observer = new IntersectionObserver(entries => { if (entries[0].isIntersecting) { currentPage++; const query = searchInput.value.trim(); if (query) { fetchMovies(query, currentPage); } } }, { threshold: 1 });
This is where the IntersectionObserver constructor function is defined. This function continues the query with the current page after the user initially fires the click event. This enables the application to fetch movie items when they need to be seen in the view-port rather than clogging the application with data, that the user may not even reach before finding the information they are looking for in this case a movie.
search.addEventListener('click', () => { currentPage = 1; // Reset to the first page on the new search movieContainer.innerHTML = ''; // Clear previous results const query = searchInput.value.trim(); if (query) { fetchMovies(query, currentPage); } });
This last code is a simple code that adds an event listener to listen for the click event on the search button element. This is like the entry point of the application, as the user enters input in the search box and clicks on the search button, the function attached to the event listener function is invoked. First, it resets the current page to 1, then clears the parsed data from the previous query. Lastly, it fetches the new query with its current page as 1. The intersection observer continues the fetching queries from the API_URL, after reaching the last-item of the parsed HTML from the Search data returned in the query.
The GIF above shows how the application works, and how the data are fetched when the user scrolls to the last movie item, this is performed by the Intersection Observer calling the fetch function after the user has queried the API.
Das Projekt zeigt, wie der Intersection Observer verwendet wird, um das untergeordnete oder Zielelement anhand des übergeordneten Elements zu beobachten. Der Beobachter ist wie ein Auge, das auf einen Kasten mit einem kleineren Kasten gesetzt wird. Wenn nun der kleinere Kasten sichtbar wird (sich schneidet), wird eine Funktion aufgerufen. In diesem Artikel ruft die aufgerufene Funktion weiterhin eine Abfrage ab, indem der Benutzer die aktuelle Seite anhängt, bis das Ende der abgerufenen und im Array analysierten Daten erreicht ist.
Der ganze Grund für den Beobachter besteht darin, das letzte Element zu beobachten, bevor er es erneut von der API_URL abruft. Diese Methode des verzögerten Ladens sowohl der Daten als auch des Posterbilds reduziert die Verschwendung von Bandbreite, erhöht die Seitenladezeit und zeigt, was noch wichtiger ist, die Benutzer nur das, was er erfragen möchte. Wenn ein Benutzer beispielsweise nach „John Wick“ fragt, die Idee aber darin besteht, „John Wick Parabellum“ zu erhalten, bevor auf der Seite alle John Wick-Filme angezeigt werden, muss der Benutzer den beabsichtigten Film vor der Suche herausgefunden haben.
Im Vergleich zum Scroll-Event-Listener ist die Intersection Observer API effizienter und einfacher zu verwenden. Das liegt daran, dass der Intersection Observer im Gegensatz zu Scroll-Ereignis-Listenern, die beim Scrollen ständig ausgelöst werden und Ihre Seite verlangsamen können, die Sichtbarkeit der Elemente nur bei Bedarf überprüft und so die Leistung verbessert. Es verarbeitet automatisch, wenn Elemente sichtbar werden, wodurch Aufgaben wie das verzögerte Laden von Bildern, das Auslösen von Animationen und das unendliche Scrollen viel einfacher und schneller zu implementieren sind. Sie müssen sich keine Gedanken über zusätzliche Coding-Tricks machen, um häufige Updates zu verwalten, und es funktioniert konsistent über verschiedene Browser hinweg.
Das obige ist der detaillierte Inhalt vonMüheloses unendliches Scrollen: So implementieren Sie Lazy Loading mit Intersection Observer. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!