paint-brush
Implementing Infinite Scroll with Vanilla JS: It's Easy as A.B.C & Dby@tingchun0113
427 reads
427 reads

Implementing Infinite Scroll with Vanilla JS: It's Easy as A.B.C & D

by TC Wang
TC Wang HackerNoon profile picture

TC Wang

@tingchun0113

Scrum/Tech PM • INSEAD MBA

September 20th, 2021
Read on Terminal Reader
Read this story in a terminal
Print this story
Read this story w/o Javascript
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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. 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](https://github.com/tingchun0113/infinite-scroll-unsplash-api).

Companies Mentioned

Mention Thumbnail
Mozilla
Mention Thumbnail
Twitter
featured image - Implementing Infinite Scroll with Vanilla JS: It's Easy as A.B.C & D
1x
Read by Dr. One voice-avatar

Listen to this story

TC Wang HackerNoon profile picture
TC Wang

TC Wang

@tingchun0113

Scrum/Tech PM • INSEAD MBA

Learn More
LEARN MORE ABOUT @TINGCHUN0113'S
EXPERTISE AND PLACE ON THE INTERNET.


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

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 an infinite scroll. If you find this article useful or have any questions, connect me on LinkedIn or follow me for more articles.

Also published here.

L O A D I N G
. . . comments & more!

About Author

TC Wang HackerNoon profile picture
TC Wang@tingchun0113
Scrum/Tech PM • INSEAD MBA

TOPICS

THIS ARTICLE WAS FEATURED IN...

Permanent on Arweave
Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite
Hashnode

Mentioned in this story

X REMOVE AD