-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (57 loc) · 2.23 KB
/
index.js
File metadata and controls
69 lines (57 loc) · 2.23 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
// Initialize cart count
let cartCount = JSON.parse(localStorage.getItem('cart'))?.length || 0;
document.getElementById('cart-count').innerText = cartCount;
// Add to Cart Function
function addToCart(productName) {
const cart = JSON.parse(localStorage.getItem('cart')) || [];
const itemIndex = cart.findIndex(item => item.name === productName);
if (itemIndex > -1) {
// If item is already in the cart, increase its quantity
cart[itemIndex].quantity += 1;
} else {
// If item is not in the cart, add it as a new entry
cart.push({ name: productName, quantity: 1 });
}
// Update local storage with the updated cart
localStorage.setItem('cart', JSON.stringify(cart));
// Update cart count based on the number of unique items in the cart
cartCount = cart.length;
document.getElementById('cart-count').innerText = cartCount;
// Show custom notification
showAddToCartNotification(`${productName} added to cart!`);
}
// Custom Notification Function for Add to Cart
function showAddToCartNotification(message) {
const notification = document.getElementById('add-to-cart-notification');
notification.innerHTML = `<span class="notification-icon">✅</span> ${message}`;
notification.classList.remove('hidden');
notification.classList.add('show');
// Hide notification after 3 seconds
setTimeout(() => {
notification.classList.remove('show');
setTimeout(() => {
notification.classList.add('hidden');
}, 300); // Wait for transition to complete
}, 3000); // Notification duration
}
// Toggle Like Function
function toggleLike(button) {
const likeCountElement = button.querySelector('.like-count');
let likeCount = parseInt(likeCountElement.innerText);
if (button.classList.contains('liked')) {
button.classList.remove('liked');
likeCount--;
} else {
button.classList.add('liked');
likeCount++;
}
likeCountElement.innerText = likeCount;
}
// Show Diwali Wish Popup
function showWish() {
document.getElementById('popup-overlay').style.display = 'flex';
}
// Close Diwali Wish Popup
function closeWish() {
document.getElementById('popup-overlay').style.display = 'none';
}