-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.js
361 lines (331 loc) · 10.6 KB
/
logic.js
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
const flashDealsProducts = [
{
image:
"https://res.cloudinary.com/dj3qdir59/image/upload/v1709756141/techmart/gaming-mouse.webp",
name: "Gaming Mouse Pro X",
price: 49.99,
originalPrice: 99.99,
discount: 50,
rating: 4.5,
reviews: 256,
sold: 3120,
},
{
image:
"https://res.cloudinary.com/dj3qdir59/image/upload/v1709756141/techmart/webcam-4k.webp",
name: "4K Webcam Ultra",
price: 79.99,
originalPrice: 159.99,
discount: 50,
rating: 4,
reviews: 167,
sold: 890,
},
{
image:
"https://res.cloudinary.com/dj3qdir59/image/upload/v1709756141/techmart/keyboard-rgb.webp",
name: "Mechanical Keyboard RGB",
price: 89.99,
originalPrice: 179.99,
discount: 50,
rating: 5,
reviews: 445,
sold: 4567,
},
{
image:
"https://res.cloudinary.com/dj3qdir59/image/upload/v1709756141/techmart/portable-ssd.webp",
name: "Portable SSD 1TB",
price: 129.99,
originalPrice: 249.99,
discount: 48,
rating: 4.5,
reviews: 334,
sold: 2234,
},
{
image:
"https://res.cloudinary.com/dj3qdir59/image/upload/v1709756141/techmart/gaming-headset.webp",
name: "Wireless Gaming Headset",
price: 69.99,
originalPrice: 139.99,
discount: 50,
rating: 4,
reviews: 223,
sold: 1678,
},
];
// Product generation helpers
const productTypes = [
"Laptop",
"Smartphone",
"Tablet",
"Smartwatch",
"Headphones",
"Speaker",
"Camera",
"Monitor",
"Keyboard",
"Mouse",
"Microphone",
"Webcam",
"Router",
"Storage",
"Power Bank",
];
const brands = [
"TechPro",
"SmartX",
"EliteGear",
"NextGen",
"PrimeTech",
"UltraMax",
"CoreTech",
"PowerTech",
"InnovatePro",
"FutureTech",
];
const features = [
"Wireless",
"Pro",
"Ultra",
"Elite",
"Premium",
"Advanced",
"Smart",
"Professional",
"Gaming",
"4K",
"HD",
"Portable",
];
const images = {
Laptop: [
"https://images.unsplash.com/photo-1603302576837-37561b2e2302",
"https://images.unsplash.com/photo-1496181133206-80ce9b88a853",
"https://images.unsplash.com/photo-1525547719571-a2d4ac8945e2",
],
Smartphone: [
"https://images.unsplash.com/photo-1598327105666-5b89351aff97",
"https://images.unsplash.com/photo-1511707171634-5f897ff02aa9",
"https://images.unsplash.com/photo-1592899677977-9c10ca588bbd",
],
Headphones: [
"https://images.unsplash.com/photo-1505740420928-5e560c06d30e",
"https://images.unsplash.com/photo-1583394838336-acd977736f90",
"https://images.unsplash.com/photo-1487215078519-e21cc028cb29",
],
Smartwatch: [
"https://images.unsplash.com/photo-1579586337278-3befd40fd17a",
"https://images.unsplash.com/photo-1508685096489-7aacd43bd3b1",
"https://images.unsplash.com/photo-1434493789847-2f02dc6ca35d",
],
Camera: [
"https://images.unsplash.com/photo-1516035069371-29a1b244cc32",
"https://images.unsplash.com/photo-1502920917128-1aa500764cbd",
"https://images.unsplash.com/photo-1581591524425-c7e0978865fc",
],
Monitor: [
"https://images.unsplash.com/photo-1527443224154-c4a3942d3acf",
"https://images.unsplash.com/photo-1551645120-d70bfe84c826",
"https://images.unsplash.com/photo-1585792180666-f7347c490ee2",
],
Keyboard: [
"https://images.unsplash.com/photo-1587829741301-dc798b83add3",
"https://images.unsplash.com/photo-1595225476474-87563907a212",
"https://images.unsplash.com/photo-1618384887929-16ec33fab9ef",
],
Mouse: [
"https://images.unsplash.com/photo-1527864550417-7fd91fc51a46",
"https://images.unsplash.com/photo-1615663245857-ac93bb7c39e7",
"https://images.unsplash.com/photo-1563297007-0686b7003af7",
],
};
function generateRandomProduct() {
const type = productTypes[Math.floor(Math.random() * productTypes.length)];
const brand = brands[Math.floor(Math.random() * brands.length)];
const feature = features[Math.floor(Math.random() * features.length)];
const name = `${brand} ${feature} ${type}`;
// Get relevant image based on product type
let image;
if (images[type]) {
const typeImages = images[type];
image = typeImages[Math.floor(Math.random() * typeImages.length)];
// Add Unsplash parameters for optimization
image += "?auto=format,compress&q=90&w=400&fit=crop";
} else {
// Fallback image if type not found
image =
"https://images.unsplash.com/photo-1526406915894-7bcd65f60845?auto=format,compress&q=90&w=400&fit=crop";
}
// Generate random price between 29.99 and 1999.99
const price = (Math.random() * (1999.99 - 29.99) + 29.99).toFixed(2);
const discount = Math.floor(Math.random() * 30) + 20; // 20-50% discount
const originalPrice = (price / (1 - discount / 100)).toFixed(2);
// Generate random rating and reviews
const rating = (Math.random() * (5 - 3.5) + 3.5).toFixed(1);
const reviews = Math.floor(Math.random() * 500) + 1;
const sold = Math.floor(Math.random() * 5000) + 100;
return {
image,
name,
price: Number(price),
originalPrice: Number(originalPrice),
discount,
rating: Number(rating),
reviews,
sold,
};
}
function generateStars(rating) {
const fullStars = Math.floor(rating);
const hasHalfStar = rating % 1 !== 0;
let stars = "★".repeat(fullStars);
if (hasHalfStar) stars += "☆";
while (stars.length < 5) stars += "☆";
return stars;
}
function createProductCard(product) {
const card = document.createElement("div");
card.className =
"product-card border rounded-lg overflow-hidden bg-white shadow-sm transition-all duration-300 ease-in-out opacity-0";
setTimeout(() => {
card.classList.remove("opacity-0");
card.classList.add("opacity-100");
}, 50);
card.innerHTML = `
<div class="relative group">
<img
src="${product.image}"
alt="${product.name}"
class="w-full h-48 object-contain p-2 transition-transform duration-300 ease-in-out group-hover:scale-105"
/>
<span class="absolute top-2 left-2 bg-red-500 text-white text-xs px-1.5 py-0.5 rounded">
${product.discount}% OFF
</span>
</div>
<div class="p-3">
<h3 class="font-medium text-sm mb-1 line-clamp-2 h-10">${
product.name
}</h3>
<div class="flex items-center mb-1">
<div class="flex text-yellow-400">${generateStars(product.rating)}</div>
<span class="ml-1 text-xs text-gray-500">(${product.reviews})</span>
</div>
<div class="flex items-center justify-between">
<div class="flex items-center gap-1">
<span class="font-bold text-base">$${product.price}</span>
<span class="text-xs text-gray-500 line-through">$${
product.originalPrice
}</span>
</div>
</div>
<p class="text-xs text-gray-500 mt-1">${product.sold.toLocaleString()} sold</p>
<button class="w-full bg-[#1a1f2c] text-white py-1.5 rounded text-sm mt-2 hover:bg-[#2a2f3c] transition-colors duration-200 ease-in-out transform hover:scale-[1.02] active:scale-[0.98]">
Add to Cart
</button>
</div>
`;
return card;
}
// Add some CSS to your HTML head
const style = document.createElement("style");
style.textContent = `
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.product-card {
transform: translateY(0);
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.product-card:hover {
transform: translateY(-8px);
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}
.product-card-animation {
animation: fadeInUp 0.6s ease-out forwards;
}
`;
document.head.appendChild(style);
// Modified insert function for initial products
const flashDealsContainer = document.querySelector("section:first-child .grid");
flashDealsProducts.forEach((product, index) => {
const card = createProductCard(product);
card.style.animationDelay = `${index * 100}ms`;
card.classList.add("product-card-animation");
flashDealsContainer.appendChild(card);
});
// Modified infinite scroll functionality
let isLoading = false;
let loadingIndicator = null;
function createLoadingIndicator() {
const indicator = document.createElement("div");
indicator.className = "flex justify-center items-center py-4 col-span-full";
indicator.innerHTML = `
<div class="animate-spin rounded-full h-8 w-8 border-b-2 border-[#1a1f2c]"></div>
`;
return indicator;
}
const loadMoreProducts = () => {
if (isLoading) return;
// Get viewport dimensions
const viewportWidth = window.innerWidth;
const viewportHeight = window.innerHeight;
// Calculate trigger point based on viewport width
// For mobile (<768px): trigger when 1.5x viewport height from bottom
// For tablets (768px-1024px): trigger when 600px from bottom
// For desktop (>1024px): trigger when 1000px from bottom
let triggerOffset;
if (viewportWidth < 768) {
triggerOffset = viewportHeight * 1.5; // This makes it trigger much earlier on mobile
} else if (viewportWidth < 1024) {
triggerOffset = 600;
} else {
triggerOffset = 1000;
}
const scrollPosition = window.innerHeight + window.scrollY;
const pageEnd = document.documentElement.offsetHeight - triggerOffset;
if (scrollPosition >= pageEnd) {
isLoading = true;
// Show loading indicator
if (!loadingIndicator) {
loadingIndicator = createLoadingIndicator();
flashDealsContainer.appendChild(loadingIndicator);
}
// Simulate network delay for smooth loading experience
setTimeout(() => {
// Generate 4 new random products
const newProducts = Array(4)
.fill()
.map(() => generateRandomProduct());
// Remove loading indicator
if (loadingIndicator) {
loadingIndicator.remove();
loadingIndicator = null;
}
// Add new products with staggered animation
newProducts.forEach((product, index) => {
const card = createProductCard(product);
card.style.animationDelay = `${index * 100}ms`;
card.classList.add("product-card-animation");
flashDealsContainer.appendChild(card);
});
isLoading = false;
}, 800);
}
};
// Debounce scroll event for better performance
let scrollTimeout;
window.addEventListener("scroll", () => {
if (scrollTimeout) {
clearTimeout(scrollTimeout);
}
scrollTimeout = setTimeout(loadMoreProducts, 100);
});