forked from mr-body/bookstoreHTML
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrolLoading.html
More file actions
57 lines (53 loc) · 1.63 KB
/
scrolLoading.html
File metadata and controls
57 lines (53 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Infinite Scrolling Example</title>
<style>
/* Some basic styles for the content */
#content {
margin-top: 20px;
}
.item {
padding: 10px;
border: 1px solid #ccc;
margin-bottom: 5px;
}
</style>
</head>
<body>
<div id="content">
<!-- Content will be dynamically added here -->
</div>
<script>
// Sample array of items
const items = Array.from({ length: 100 }, (_, i) => `Item ${i + 1}`);
// Function to render items
function renderItems(start, end) {
const contentDiv = document.getElementById('content');
for (let i = start; i < end; i++) {
const itemDiv = document.createElement('div');
itemDiv.classList.add('item');
itemDiv.textContent = items[i];
contentDiv.appendChild(itemDiv);
}
}
// Initial render
renderItems(0, 10);
// Function to load more items when scrolling
window.addEventListener('scroll', () => {
const contentDiv = document.getElementById('content');
const scrollTop = document.documentElement.scrollTop;
const windowHeight = window.innerHeight;
const documentHeight = document.documentElement.scrollHeight;
if (scrollTop + windowHeight >= documentHeight - 100) {
// Load more items
const currentItemCount = contentDiv.children.length;
const newEndIndex = Math.min(currentItemCount + 10, items.length);
renderItems(currentItemCount, newEndIndex);
}
});
</script>
</body>
</html>