-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_more_Ajax.html
213 lines (194 loc) · 5.34 KB
/
load_more_Ajax.html
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<!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>Load more - Jquery & Ajax</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.product-container {
list-style-type: none;
max-width: 600px;
width: 100%;
margin: 1rem 0;
}
.product-box {
position: relative;
display: flex;
align-items: center;
padding: 0.5rem 1rem;
background-color: #eee;
transition: transform 0.3s ease;
}
.product-box:hover {
transform: scale(1.1);
}
.product-num {
position: absolute;
right: calc(100% + 20px);
border-radius: 50%;
background-color: #eee;
width: 60px;
height: 60px;
text-align: center;
line-height: 60px;
font-size: 1.6rem;
font-weight: 600;
}
.product-box + .product-box {
margin-top: 1rem;
}
.product-photo {
width: 100px;
height: 100px;
}
.product-photo img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 50%;
}
.product-content {
flex: 1 1 0;
margin-left: 1rem;
}
.rating {
float: left;
margin-top: 0.5rem;
}
.create-from-now {
float: right;
margin-top: 0.5rem;
}
.go-to-top {
position: fixed;
bottom: 1rem;
right: 1rem;
padding: 0.5rem 1rem;
border-radius: 4px;
background-color: #aaa;
cursor: pointer;
}
.load-more {
padding: 0.5rem 1rem;
border-radius: 4px;
background-color: #eee;
cursor: pointer;
}
</style>
</head>
<body>
<div><ul class="product-container"></ul></div>
<div class="load-more">Load more</div>
<div class="go-to-top">Go To Top</div>
<script
src="https://code.jquery.com/jquery-3.6.2.min.js"
integrity="sha256-2krYZKh//PcchRtd+H+VyyQoZ/e3EcrkxhM8ycwASPA="
crossorigin="anonymous"
></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
<script>
// define global variables
var productPage = 1;
var loadMoreBtn = $(".load-more");
var prevText = loadMoreBtn.text();
var productFetching = false;
var showGoToTop = 300;
var disableLoadMore = false;
// document ready
$(function () {
getProducts();
});
$(window).scroll(function () {
// show/hide go to top button
var scrollTop = $(this).scrollTop();
if (scrollTop >= showGoToTop) {
$(".go-to-top").fadeIn();
} else {
$(".go-to-top").fadeOut();
}
// products infinite loading
var windowHeight = $(this).height();
var documentHeight = $(document).height();
if (windowHeight + scrollTop >= documentHeight - 200) {
!disableLoadMore && getProductNextPage();
}
});
$(".go-to-top").click(function () {
$("html, body").animate({ scrollTop: 0 }, "slow");
});
loadMoreBtn.click(function () {
if (productFetching) return;
productPage++;
getProducts(productPage);
});
function getProductNextPage() {
if (productFetching) return;
productPage++;
getProducts(productPage);
}
function getProducts(page) {
if (!page) {
page = 1;
}
// show loading
loadMoreBtn.text("Loading...");
productFetching = true;
$.ajax({
type: "GET",
url: `https://dummyjson.com/products?limit=10&skip=${
(page - 1) * 10
}`,
dataType: "json",
success: function (res) {
// auto hide .load-more button
if (res.products.length === 0) {
loadMoreBtn.fadeOut();
disableLoadMore = true;
}
appendProducts(res.products);
loadMoreBtn.text(prevText);
productFetching = false;
},
});
}
function appendProducts(products) {
var html = "";
$.each(products, function (idx, product) {
html += ` <li class="product-box">
<div class="product-num">${(productPage - 1) * 10 + idx + 1}</div>
<div class="product-photo"><img src="${
product.thumbnail
}" alt=""></div>
<div class="product-content">
<h3 class="product-title">
${product.title}
</h3>
<p class="product-description">
${product.description}
</p>
<p class="rating">
Rating: ${product.rating}
</p>
<p class="create-from-now">
$${product.price}
</p>
</div>
</li>`;
});
$(".product-container").append(html);
}
</script>
</body>
</html>