-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInfiniteScroll.html
More file actions
105 lines (94 loc) · 2.57 KB
/
InfiniteScroll.html
File metadata and controls
105 lines (94 loc) · 2.57 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>NativeValidationMessage</title>
<style>
.card-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.card {
width: 22rem;
height: 6rem;
background-color: antiquewhite;
border-radius: 6px;
margin: 0.5rem;
}
#loader {
opacity: 0;
position: fixed;
left: 50%;
bottom: 50px !important;
z-index: 999;
transform: translateX(50%);
}
.load {
opacity: 1 !important;
animation: spin;
animation-duration: 2s;
animation-iteration-count: infinite;
animation-timing-function: ease-in-out;
}
@keyframes spin {
from {
transform: scale(1);
}
50% {
transform: scale(1.25);
}
to {
transform: scale(1);
}
}
</style>
</head>
<body>
<h2 style="text-align: center">Infinite Scroll</h2>
<br />
<div class="card-container">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
</div>
<div id="loader">Loading</div>
</body>
<script>
const fetchData = function () {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(10);
}, 2000);
});
};
const renderRows = (rowsCount) => {
console.log("render rows called with" + rowsCount);
let cardContainer = document.getElementsByClassName("card-container")[0];
let newCard = document.createElement("div");
newCard.classList.add("card");
Array(rowsCount)
.fill(1)
.map(() => {
cardContainer.append(newCard);
});
document.getElementById("loader").classList.remove("load");
};
const loadMoreData = async function (event) {
const { scrollTop, clientHeight, scrollHeight } =
document.documentElement;
if (scrollTop + clientHeight > scrollHeight - 20) {
document.getElementById("loader").classList.add("load");
let data = await fetchData().then((rowsCount) => renderRows(rowsCount));
}
};
document.addEventListener("scroll", loadMoreData);
</script>
</html>