How to Implement Infinite Scroll with Vanilla JS

Infinite scroll is often used on social media sites such as Twitter or Pinterest. The feature allows users to load some pictures/contents on a website and then load more once reaching the end of the webpage. 

I used Unsplash API to get random pictures…


This content originally appeared on DEV Community and was authored by TC Wang

Infinite scroll is often used on social media sites such as Twitter or Pinterest. The feature allows users to load some pictures/contents on a website and then load more once reaching the end of the webpage. 

I used Unsplash API to get random pictures. This article will focus on how to use JavaScript to make use of some properties to achieve infinite scroll. You can find other project files (HTML or CSS files) in this repo.

Four Properties to Achieve Infinite Scroll

A) window.scrollY: How far the document has been scrolled from the top
B) window.innerHeight: The visible part of the window
C) document.body.offsetHeight: The height of the entire document
D) 1000px (or any value): The distance from bottom of document

The diagram below better illustrates these properties:
JavaScript Web Projects: 20 Projects to Build Your Portfolio

Looking at the above, we can listen to the scroll event:

If A (scrollY) + B (innerHeight) >= C (document height) - D (1000px) 
-> load more photos
// Check to see if scrolling near bottom of page; load more photos
window.addEventListener('scroll', () => {
  if (
    window.scrollY + window.innerHeight >= document.body.offsetHeight - 1000
  ) {
    getPhotos();
  }
});

Final Thoughts

There're other tools (Intersection Observer API) to implement infinite scroll. If you find this article useful or have any questions, connect me on LinkedIn or follow me on Medium for more articles.


This content originally appeared on DEV Community and was authored by TC Wang


Print Share Comment Cite Upload Translate Updates
APA

TC Wang | Sciencx (2021-08-24T14:43:37+00:00) How to Implement Infinite Scroll with Vanilla JS. Retrieved from https://www.scien.cx/2021/08/24/how-to-implement-infinite-scroll-with-vanilla-js/

MLA
" » How to Implement Infinite Scroll with Vanilla JS." TC Wang | Sciencx - Tuesday August 24, 2021, https://www.scien.cx/2021/08/24/how-to-implement-infinite-scroll-with-vanilla-js/
HARVARD
TC Wang | Sciencx Tuesday August 24, 2021 » How to Implement Infinite Scroll with Vanilla JS., viewed ,<https://www.scien.cx/2021/08/24/how-to-implement-infinite-scroll-with-vanilla-js/>
VANCOUVER
TC Wang | Sciencx - » How to Implement Infinite Scroll with Vanilla JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/24/how-to-implement-infinite-scroll-with-vanilla-js/
CHICAGO
" » How to Implement Infinite Scroll with Vanilla JS." TC Wang | Sciencx - Accessed . https://www.scien.cx/2021/08/24/how-to-implement-infinite-scroll-with-vanilla-js/
IEEE
" » How to Implement Infinite Scroll with Vanilla JS." TC Wang | Sciencx [Online]. Available: https://www.scien.cx/2021/08/24/how-to-implement-infinite-scroll-with-vanilla-js/. [Accessed: ]
rf:citation
» How to Implement Infinite Scroll with Vanilla JS | TC Wang | Sciencx | https://www.scien.cx/2021/08/24/how-to-implement-infinite-scroll-with-vanilla-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.