-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
133 lines (110 loc) · 4.45 KB
/
script.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
document.addEventListener("DOMContentLoaded", function () {
// Smooth Scroll
const navLinks = document.querySelectorAll('.site-header nav ul li a');
navLinks.forEach(link => {
link.addEventListener('click', function (e) {
e.preventDefault();
const targetId = this.getAttribute('href').substring(1);
const targetSection = document.getElementById(targetId);
window.scrollTo({
top: targetSection.offsetTop - document.querySelector('.site-header').offsetHeight,
behavior: 'smooth'
});
});
});
// Popup Notification as Cart
const addToCartButtons = document.querySelectorAll('.product .btn');
const cartPopup = document.querySelector('.cart-popup');
const cartItems = document.getElementById('cart-items');
const clearCartBtn = document.getElementById('clear-cart-btn');
const checkoutBtn = document.getElementById('checkout-btn');
const cartToggle = document.querySelector('.cart-toggle');
let cart = JSON.parse(localStorage.getItem('cart')) || [];
addToCartButtons.forEach(button => {
button.addEventListener('click', (e) => {
const product = e.target.closest('.product');
const productName = product.querySelector('h3').innerText;
const productPrice = product.querySelector('p').innerText;
const cartItem = {
name: productName,
price: productPrice,
quantity: 1 // For simplicity, assuming quantity is always 1 initially
};
// Check if item already exists in cart
const existingItem = cart.find(item => item.name === cartItem.name);
if (existingItem) {
existingItem.quantity++;
} else {
cart.push(cartItem);
}
// Save cart to local storage
localStorage.setItem('cart', JSON.stringify(cart));
// Update cart display
updateCart();
showCartNotification();
});
});
function updateCart() {
cartItems.innerHTML = '';
cart.forEach(item => {
const li = document.createElement('li');
li.innerHTML = `${item.name} - ${item.price} - Quantity: ${item.quantity}`;
cartItems.appendChild(li);
});
// Update cart popup count
cartToggle.innerText = `Cart (${cart.length})`;
}
function showCartNotification() {
cartPopup.classList.add('show');
setTimeout(() => {
cartPopup.classList.remove('show');
}, 3000); // Hide after 3 seconds
}
// Clear Cart
clearCartBtn.addEventListener('click', () => {
cart = [];
localStorage.removeItem('cart');
updateCart();
});
// Checkout Button (clears cart for demo purposes)
checkoutBtn.addEventListener('click', () => {
alert('Checkout functionality to be implemented.');
cart = [];
localStorage.removeItem('cart');
updateCart();
});
// Initialize Cart on Page Load
updateCart();
// Dark Mode Toggle (for demonstration purposes)
const body = document.querySelector('body');
const darkModeToggle = document.getElementById('dark-mode-toggle');
darkModeToggle.addEventListener('click', () => {
body.classList.toggle('light-mode');
});
// User Personalization through Cookies (dummy implementation)
function setCookie(name, value, days) {
const expires = new Date();
expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000);
document.cookie = `${name}=${value};expires=${expires.toUTCString()};path=/`;
}
function getCookie(name) {
const cookieName = `${name}=`;
const cookieArray = document.cookie.split(';');
for (let i = 0; i < cookieArray.length; i++) {
let cookie = cookieArray[i].trim();
if (cookie.startsWith(cookieName)) {
return cookie.substring(cookieName.length, cookie.length);
}
}
return null;
}
const userTheme = getCookie('user_theme');
if (userTheme) {
body.classList.add(userTheme);
}
darkModeToggle.addEventListener('click', () => {
body.classList.toggle('light-mode');
const theme = body.classList.contains('light-mode') ? 'light-mode' : '';
setCookie('user_theme', theme, 30); // Set cookie for 30 days
});
});