Infinity Scroll monitors the user’s scroll position on every scroll event and automatically fetches a new batch of photos from the Unsplash API before the user ever reaches the bottom of the page. There are no “Load More” buttons and no page numbers — the gallery grows continuously as long as the user keeps scrolling.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/ClaytonSeager/InifinityScroll/llms.txt
Use this file to discover all available pages before exploring further.
The scroll trigger
A singlescroll listener on window is responsible for deciding when to load the next batch. The condition combines three measurements of the page’s current state:
| Expression | What it measures |
|---|---|
innerHeight | The visible height of the browser viewport in pixels |
scrollY | How far down the page the user has scrolled |
innerHeight + scrollY | The pixel position of the bottom edge of the viewport |
offsetHeight | The full rendered height of the <body> element |
offsetHeight - 1000 | A point 1000px above the very bottom of the page |
innerHeight + scrollY reaches or passes offsetHeight - 1000, the bottom of the viewport is within 1000 pixels of the end of the page. The second condition, && ready, ensures a fetch is not triggered while one is already in progress.
The ready flag
The ready boolean is the gatekeeper that prevents multiple concurrent fetches from firing at the same time.
- Starts as
false— the app is not ready to scroll-trigger a fetch until the first batch of images has fully loaded. - Set to
falseimmediately when the scroll condition is met, blocking any further triggers for the duration of the fetch. - Set back to
trueonly once every image in the current batch has fired itsloadevent (insideimageLoaded()).
Fetch cycle
The complete journey from scroll to rendered images follows these steps:Unsplash API responds
The response JSON is parsed. For search results,
data.results is used; for random photos, data is used directly.displayPhotos() renders images
Each photo in the array is turned into an
<a> + <img> element and appended to #Image__Container.Each image fires its load event
As each
<img> finishes downloading, its load event calls imageLoaded().imageLoaded() increments the counter
imagesLoaded and totalLoadedImages are both incremented; the on-screen counter updates.Image loading tracking
Three variables work together to track batch progress:| Variable | Role |
|---|---|
totalImages | Set at the start of displayPhotos() to photosArray.length — the number of images in the current batch |
imagesLoaded | Counts how many images in the current batch have finished loading; reset to 0 after each complete batch |
totalLoadedImages | A running total of every image loaded across all batches, displayed in the on-screen counter |
imageLoaded function handles all three:
<img> element registers this callback through an inline event listener added in displayPhotos():
imageLoaded() does imagesLoaded === totalImages become true, flipping ready back to true and hiding the loader overlay.