-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
247 lines (209 loc) · 6.83 KB
/
script.js
File metadata and controls
247 lines (209 loc) · 6.83 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
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
// =====================
// WAIT FOR HTML TO LOAD
// =====================
document.addEventListener("DOMContentLoaded", function () {
// =====================
// LENIS SMOOTH SCROLL
// =====================
const lenis = new Lenis({
duration: 1.2,
easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
direction: 'vertical',
gestureDirection: 'vertical',
smooth: true,
mouseMultiplier: 1,
smoothTouch: false,
touchMultiplier: 2,
});
function raf(time) {
lenis.raf(time);
requestAnimationFrame(raf);
}
requestAnimationFrame(raf);
// =====================
// CART STATE
// =====================
let cart = [];
// =====================
// DOM ELEMENTS
// =====================
const buttons = document.querySelectorAll(".add-to-cart");
const cartDisplay = document.getElementById("cart-count");
const cartList = document.getElementById("cart-list");
const cartTotal = document.getElementById("cart-total");
const clearCartBtn = document.getElementById("clear-cart");
// =====================
// SAVE CART TO LOCAL STORAGE
// =====================
function saveCart() {
localStorage.setItem("cart", JSON.stringify(cart));
}
// =====================
// RENDER CART UI
// =====================
function renderCart() {
cartList.innerHTML = "";
let totalPrice = 0;
cart.forEach(function (item, index) {
const itemTotal = item.price * item.quantity;
totalPrice += itemTotal;
const li = document.createElement("li");
li.innerHTML = `
${item.name} — ₹${item.price} × ${item.quantity}
<button class="qty-btn minus" data-index="${index}">➖</button>
<button class="qty-btn plus" data-index="${index}">➕</button>
<button class="remove-btn" data-index="${index}">❌</button>
<strong> = ₹${itemTotal}</strong>
`;
cartList.appendChild(li);
});
cartTotal.textContent = totalPrice;
attachQuantityEvents();
attachRemoveEvents();
}
// =====================
// REMOVE ITEM FROM CART
// =====================
function attachRemoveEvents() {
document.querySelectorAll(".remove-btn").forEach(button => {
button.addEventListener("click", function () {
const index = Number(button.dataset.index);
cart.splice(index, 1);
updateCartCount();
renderCart();
saveCart();
});
});
}
// =====================
// INCREASE / DECREASE QUANTITY
// =====================
function attachQuantityEvents() {
document.querySelectorAll(".plus").forEach(button => {
button.addEventListener("click", function () {
const index = Number(button.dataset.index);
cart[index].quantity += 1;
updateCartCount();
renderCart();
saveCart();
});
});
document.querySelectorAll(".minus").forEach(button => {
button.addEventListener("click", function () {
const index = Number(button.dataset.index);
cart[index].quantity -= 1;
if (cart[index].quantity <= 0) {
cart.splice(index, 1);
}
updateCartCount();
renderCart();
saveCart();
});
});
}
// =====================
// UPDATE CART COUNT
// =====================
function updateCartCount() {
const totalItems = cart.reduce((sum, item) => sum + item.quantity, 0);
cartDisplay.textContent = totalItems;
const navCount = document.getElementById("cart-count-nav");
if (navCount) navCount.textContent = totalItems;
}
// =====================
// LOAD CART FROM STORAGE
// =====================
const savedCart = localStorage.getItem("cart");
if (savedCart) {
cart = JSON.parse(savedCart);
updateCartCount();
renderCart();
}
// =====================
// CLEAR CART
// =====================
clearCartBtn.addEventListener("click", function () {
cart = [];
cartDisplay.textContent = 0;
cartList.innerHTML = "";
cartTotal.textContent = 0;
localStorage.removeItem("cart");
});
// =====================
// ADD TO CART BUTTONS
// =====================
// =====================
// ADD TO CART (EVENT DELEGATION)
// =====================
document.body.addEventListener("click", function (e) {
if (e.target.classList.contains("add-to-cart")) {
const button = e.target;
const productName = button.dataset.name;
const productPrice = Number(button.dataset.price);
const existingProduct = cart.find(item => item.name === productName);
if (existingProduct) {
existingProduct.quantity += 1;
} else {
cart.push({
name: productName,
price: productPrice,
quantity: 1
});
}
updateCartCount();
renderCart();
saveCart();
// Optional: visual feedback
const originalText = button.innerText;
button.innerText = "Added!";
setTimeout(() => button.innerText = originalText, 1000);
}
});
// =====================
// GSAP SCROLL ANIMATIONS
// =====================
gsap.registerPlugin(ScrollTrigger);
gsap.utils.toArray(".fade-in").forEach((el) => {
gsap.from(el, {
scrollTrigger: {
trigger: el,
start: "top 85%",
toggleActions: "play none none none"
},
y: 50,
opacity: 0,
duration: 0.6,
ease: "power2.out"
});
});
});
// =====================
// GSAP HERO INTRO
// =====================
gsap.from(".hero-tag", {
y: -20,
opacity: 0,
duration: 0.6,
ease: "power2.out"
});
gsap.from(".hero-title", {
y: 50,
opacity: 0,
duration: 0.8,
delay: 0.2,
ease: "power3.out"
});
gsap.from(".hero-subtitle", {
y: 40,
opacity: 0,
duration: 0.8,
delay: 0.4,
ease: "power3.out"
});
gsap.from(".hero-btn", {
y: 30,
opacity: 0,
duration: 0.6,
delay: 0.6,
ease: "power2.out"
});