Did you notice there are websites where you can’t seem to finish scrolling? There’s always new content available when you scroll down.
An example is Twitter.
This pattern is called an infinite scroll.
Anatomy of an infinite scroll
An infinite scroll requires 3 things to work:
A JavaScript API to fetch content
A Spinner that spins when we’re fetching content
A section in the DOM to add new content
HTML and CSS
We will build an infinite scroll that looks like this together in the next few lessons:
In this case, we will load a bunch of images from Dribbble. Each image is called a “shot” in Dribbble’s language. We will include three things in each shot:
If you place multiple copies of the first shot into the DOM, you should see a grid of shots.
The spinner
There is a spinner for this component. This spinner is already in the HTML file. You should see it spinning like this:
Get more images section
For this component, we will fetch 36 Dribbble shots with a custom API I created. After showing all 36 shots, we want to direct people to Dribbble for more shots.
The dribbble section does this.
We can hide this section with the hidden attribute since we won’t show it at first.
Dribbble has an API. But we cannot use it for this course since it requires OAuth authentication. (We cannot use OAuth with a browser. We can only use it with a server, which is out of scope for this course).
Since we can’t use Dribbble’s API directly, I built a custom API for this component. This API contains a set of 36 images. One image for each alphabet (from A to Z) and one image for each number (from 0 to 9).
Endpoint
The endpoint for this API is
https://api.learnjavascript.today/letters
This endpoint responds with a letters object. This letters object contains an array of 36 letters. Each letter object contains 4 properties:
letter: The letter
creator: Name of the creator as stated on Dribbble
You can send two query parameters to the endpoint:
limit
page
limit lets you limit the number of shots. For example, if you set limit to 3, you will only get back 3 shots out of 36 shots.
page lets you decide which set of characters you get. It works together with limit. For example:
limit=3 and page=1 gives you A, B, C
limit=3 and page=2 gives you D, E, F
limit=3 and page=3 gives you G, H, I
And so on
The API returns previousPage and nextPage properties when limit and page are used. This lets you fetch the next set (or previous set) of letters easily.
That’s everything you need to know to build the Infinite Scroll component. We’ll build the component together in the next lesson. Try building the component yourself before you read the next lesson!