How to create Infinite Scrolling using onScroll Event in JavaScript? Last Updated : 25 Jul, 2024 Comments Improve Suggest changes Like Article Like Report We have seen many shopping sites that list some limited number of products(using a back-end API) when we open the website and scroll down to the bottom, we see that more products are fetched and displayed, and as we scroll even further even more products are loaded and fetched and it keeps happening till there are no more products to be displayed. This is something that we call Infinite Scrolling. We will implement this similar feature in Javascript using the onScroll event.Now let's implement Infinite Scrolling.Approach:We will declare a variable pageNum and initialize its value as 1.We will create a scrollable div element with the class container, inside this div we will show all the results fetched from an API. We will use a paginated API with endpoint https://api.github.com/repositories/1300192/issues?page=${pageNum}.We will create a function fetchData which will be responsible for fetching data from the API.To this container element, we will attach onscroll event and we will check that when we have reached the end of the container we will increment pageNum by 1 and fetch more data by calling the function fetchData. We will check that if Math.ceil(container.clientHeight + container.scrollTop) >= container.scrollHeight then it means that we have reached the end of the container. HTML <!DOCTYPE html> <html> <head> <title>Page Title</title> <link rel="stylesheet" href="./src/styles.css" /> </head> <body> <div class="container"> <h1>GeeksforGeeks Infinite Scrolling</h1> <div class="result-container"></div> <div class="loading-container"></div> </div> <script src="src/index.js"></script> </body> </html> CSS body { font-family: sans-serif; background-color: green; } .container { background-color: white; width: 500px; height: 300px; border: 2px solid black; position: fixed; left: 50%; transform: translateX(-50%); padding: 30px; overflow-y: scroll; } .loading-container { margin-top: 10px; font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif; font-size: 30px; text-align: center; } .search-data { margin-top: 10px; height: 30px; font-family: Verdana, Geneva, Tahoma, sans-serif; } JavaScript let pageNum = 1; let isLoading = false; const container = document.querySelector(".container"); const loadingContainer = document.querySelector(".loading-container"); const resultContainer = document.querySelector(".result-container"); function fetchData() { if (isLoading) return; isLoading = true; loadingContainer.innerHTML = "Loading ..."; fetch(`https://api.../issues?page=${pageNum}`) .then((res) => { res.json().then((finalRes) => { finalRes.forEach((item) => { const resultDiv = document.createElement("div"); resultDiv.textContent = item.title; resultDiv.classList.add("search-data"); resultContainer.appendChild(resultDiv); }); isLoading = false; loadingContainer.innerHTML = ""; }) .catch((e) => { isLoading = false; loadingContainer.innerHTML = ""; }); }) .catch((err) => { isLoading = false; loadingContainer.innerHTML = ""; }); } container.onscroll = () => { if (isLoading) return; if ( Math.ceil(container.clientHeight + container.scrollTop) >= container.scrollHeight ) { pageNum++; fetchData(); } }; fetchData(); Output:Explanation: For the first time some result is fetched and displayed and as we scroll down further more data is fetched and displayed. Comment More infoAdvertise with us Next Article How to create Infinite Scrolling using onScroll Event in JavaScript? S sakshi_srivastava Follow Improve Article Tags : Project JavaScript Web Technologies JavaScript-Questions Similar Reads How to Scroll to an Element Inside a Div using JavaScript? To scroll to an element within a div using JavaScript, set the parent div's scrollTop`to the target element's offsetTop. This method allows smooth navigation within a scrollable container. Below are the methods to scroll to an element inside a Div using JavaScript:Table of Content Using scrollIntoVi 3 min read Create an Infinite Scroll Page using HTML CSS & JavaScript In this article, we will create an infinite scroll page using HTML, CSS, and JavaScript. Infinite scrolling allows you to load and display content as the user scrolls down the page, providing a seamless browsing experience. We'll fetch and append new content dynamically as the user reaches the end o 2 min read How to Create Scroll Indicator using HTML CSS and JavaScript ? Scroll Indicator is a progress bar that represents how much a page has been scrolled. When we scroll down the bar fills up and when we scroll up the bar amount reduces. Approach: Now, we will create a basic webpage with text to enable scrolling and then use JavaScript to make the scroll indicator wo 3 min read How to Zoom an Image on Scroll using JavaScript? Zoom in and out of images is a common feature in web development. One way to achieve this effect is by using JavaScript to zoom in and out of an image when the user scrolls. In this article, we will go through the steps needed to create a zoomable image using JavaScript.Step 1: HTML Markup: The firs 3 min read How to Draw on Scroll using JavaScript and SVG ? To create an engaging scroll animation, use JavaScript to manipulate SVG path properties. As the user scrolls, JavaScript calculates the scroll percentage and adjusts the strokeDasharray and strokeDashoffset of the SVG path, gradually drawing the path to create a dynamic visual effect. ApproachCreat 2 min read Like