Infinite Scrolling in Websites - Comprehensive Guide
Infinite Scrolling in Websites - Comprehensive Guide
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
1
Example 1: Simple Infinite Scrolling with JavaScript 3
Advanced Infinite Scrolling with APIs 4
Example 2: Fetching Data from an API 4
Exercises 6
Exercise 1: Implement Basic Infinite Scrolling 6
Solution: 6
Exercise 2: Use an API for Infinite Scrolling 6
Solution: 6
Exercise 3: Add a "Load More" Button 7
Solution: 7
Multiple-Choice Questions 7
Question 1: 7
Question 2: 8
Question 3: 8
Best Practices for Infinite Scrolling 8
Infinite scrolling is a web design pattern that dynamically loads additional content as the user
scrolls down a page. This guide explains the concept, provides examples, exercises, and
multiple-choice questions to help you implement infinite scrolling on your website.
Infinite scrolling continuously loads new data when the user reaches the end of the current
content, creating a seamless browsing experience.
Advantages:
Disadvantages:
1. Trigger Detection: Detect when the user reaches the end of the current content (e.g.,
using the scroll event).
2. Fetch New Content: Load new data using APIs or a backend service.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
2
3. Append Content: Dynamically add the new content to the page.
3
loadingDiv.style.display = 'block';
setTimeout(() => {
for (let i = 1; i <= 10; i++) {
const item = document.createElement('div');
item.className = 'item';
item.textContent = `Item ${i + (page - 1) * 10}`;
contentDiv.appendChild(item);
}
loadingDiv.style.display = 'none';
}, 1000);
}
// Infinite scroll detection
window.addEventListener('scroll', () => {
if (window.innerHeight + window.scrollY >=
document.body.offsetHeight - 100) {
currentPage++;
loadContent(currentPage);
}
});
// Initial content load
loadContent(currentPage);
</script>
</body>
</html>
Explanation:
● Scroll Event: Detects when the user reaches near the bottom of the page.
● Content Loader: Simulates loading 10 items with a delay.
● Dynamic Updates: Appends new items to the content container.
4
#content {
width: 300px;
margin: 0 auto;
}
.item {
padding: 20px;
margin: 10px 0;
background: lightcoral;
border: 1px solid #ddd;
}
#loading {
text-align: center;
margin-top: 20px;
display: none;
}
</style>
</head>
<body>
<div id="content">
<!-- Dynamic content will be appended here -->
</div>
<div id="loading">Loading...</div>
<script>
let currentPage = 1;
const contentDiv = document.getElementById('content');
const loadingDiv = document.getElementById('loading');
// Function to load data from API
async function loadContent(page) {
loadingDiv.style.display = 'block';
const response = await
fetch(`https://jsonplaceholder.typicode.com/posts?_page=${page}&_limit
=10`);
const data = await response.json();
data.forEach(post => {
const item = document.createElement('div');
item.className = 'item';
item.textContent = post.title;
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
5
contentDiv.appendChild(item);
});
loadingDiv.style.display = 'none';
}
// Infinite scroll detection
window.addEventListener('scroll', () => {
if (window.innerHeight + window.scrollY >=
document.body.offsetHeight - 100) {
currentPage++;
loadContent(currentPage);
}
});
// Initial content load
loadContent(currentPage);
</script>
</body>
</html>
Explanation:
Exercises
Create an infinite scrolling web page that loads 5 new items every time the user reaches the
bottom.
Solution:
Solution:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
6
1. Update the API URL to https://jsonplaceholder.typicode.com/users.
2. Display name and email in the content area.
Instead of infinite scrolling, create a "Load More" button to fetch new content.
Solution:
Add a button:
document.getElementById('loadMore').addEventListener('click', () => {
currentPage++;
loadContent(currentPage);
});
Multiple-Choice Questions
Question 1:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
7
Question 2:
Which JavaScript API is commonly used to fetch new content for infinite scrolling?
1. XMLHttpRequest
2. fetch
3. document.querySelector
4. JSON.stringify
Answer: 2. fetch
Question 3:
What event is used to detect when a user scrolls down the page?
1. onScroll
2. scroll
3. onload
4. mousemove
Answer: 2. scroll
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis