Step 3: Create your CSS file

For this example, we'll keep the stylesheet very simple.

.container {  display: flex;  justify-content: center;  align-items: center;  flex-wrap: wrap;}.header {  font-size: 32px;  color: black;  text-align: center;  font-family: Verdana, sans-serif;}img {  width: 400px;  height: 400px;  margin: 4px;  object-fit: cover;}
Step 4: Create your JavaScript file

Select the container and get the Random Fox API URL. Don't forget to link your JavaScript and CSS files in the HTML.

const container = document.querySelector(\\'.container\\');const URL = \\\"https://randomfox.ca/images/\\\";function getRandomNumber() {  return Math.floor(Math.random() * 100);}function loadImages(numImages = 6) {  let i = 0;  while (i < numImages) {    const img = document.createElement(\\'img\\');    img.src = `${URL}${getRandomNumber()}.jpg`;    container.appendChild(img);    i  ;  }}loadImages();
Step 5: Add the Scroll Event

To implement infinite scroll functionality, add this event listener:

window.addEventListener(\\'scroll\\', () => {  if (window.scrollY   window.innerHeight >= document.documentElement.scrollHeight) {    loadImages();  }});

If the sum of scrollY and innerHeight is greater than or equal to scrollHeight, it means that we have reached the end of the document and therefore need to load more images.

Conclusion

Your page should now be functional with the infinite scroll technique. Here's a challenge for you: try to redo this project using another API of your choice, also implement some more elaborate style to display your items. Good luck!

","image":"http://www.luping.net/uploads/20240823/172438812366c8131bbda9a.jpg","datePublished":"2024-08-23T12:42:03+08:00","dateModified":"2024-08-23T12:42:03+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Understanding and Implementing Infinite Scroll with JavaScript

Understanding and Implementing Infinite Scroll with JavaScript

Published on 2024-08-23
Browse:953

Entendendo e Implementando o Scroll Infinito com JavaScript

What is Infinite Scroll?

You may have already seen sites like online stores where, as you scroll down the page, products appear continuously. Another example is endless.horse, it may seem simple but it is an excellent example to demonstrate the functionality of "Infinite Scrolls". When you access the website, you see a horse, but when you scroll down the page, you realize that the horse's legs continue to grow indefinitely, and you never get to the horse's feet.

Implementing Infinite Scroll

This functionality is widely used in modern development. We can see infinite scroll in action on major social networks, such as Twitter, Facebook and especially Instagram, where the content never seems to end as we scroll down the page.

In this article, I will demonstrate a basic implementation of this functionality. However, this approach does not address issues such as issues with long queries, caching implementation, and other solutions required for production applications. Still, this is a starting point for understanding how we can implement this feature.

Step 1: Decide where to add infinite scroll functionality

First, decide where you will add the infinite scroll functionality. Will it be a list of posts or images? You will also need to decide where the data will come from. For this example, we will use images from a basic API, the Random Fox API.

Step 2: Create your HTML file

Create the HTML file and include a container for your random fox images.



Fox Images

Fox Images

Step 3: Create your CSS file

For this example, we'll keep the stylesheet very simple.

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
}

.header {
  font-size: 32px;
  color: black;
  text-align: center;
  font-family: Verdana, sans-serif;
}

img {
  width: 400px;
  height: 400px;
  margin: 4px;
  object-fit: cover;
}
Step 4: Create your JavaScript file

Select the container and get the Random Fox API URL. Don't forget to link your JavaScript and CSS files in the HTML.

const container = document.querySelector('.container');
const URL = "https://randomfox.ca/images/";

function getRandomNumber() {
  return Math.floor(Math.random() * 100);
}

function loadImages(numImages = 6) {
  let i = 0;
  while (i 



Step 5: Add the Scroll Event

To implement infinite scroll functionality, add this event listener:

window.addEventListener('scroll', () => {
  if (window.scrollY   window.innerHeight >= document.documentElement.scrollHeight) {
    loadImages();
  }
});

If the sum of scrollY and innerHeight is greater than or equal to scrollHeight, it means that we have reached the end of the document and therefore need to load more images.

Conclusion

Your page should now be functional with the infinite scroll technique. Here's a challenge for you: try to redo this project using another API of your choice, also implement some more elaborate style to display your items. Good luck!

Release Statement This article is reproduced at: https://dev.to/josimar_canejo/entendendo-e-implementando-o-scroll-infinito-com-javascript-4526?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3