-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
189 lines (112 loc) · 3.46 KB
/
script.js
File metadata and controls
189 lines (112 loc) · 3.46 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
let total = 0;
let cartCount = 0;
/* ADD TO CART FUNCTION */
function addToCart(productName, productPrice){
let cart =
JSON.parse(localStorage.getItem("cart")) || [];
cart.push({
name: productName,
price: productPrice
});
localStorage.setItem(
"cart",
JSON.stringify(cart)
);
cartCount++;
document.getElementById("cart-count")
.innerText = cartCount;
showNotification(productName + " added to cart");
}
/* NOTIFICATION FUNCTION */
function showNotification(message){
const notification = document.createElement("div");
notification.innerText = message;
notification.style.position = "fixed";
notification.style.top = "20px";
notification.style.right = "20px";
notification.style.background = "#2563eb";
notification.style.color = "white";
notification.style.padding = "15px 25px";
notification.style.borderRadius = "10px";
notification.style.boxShadow = "0 5px 15px rgba(0,0,0,0.2)";
notification.style.zIndex = "9999";
notification.style.fontWeight = "500";
notification.style.opacity = "0";
notification.style.transform = "translateY(-20px)";
notification.style.transition = "0.4s ease";
document.body.appendChild(notification);
setTimeout(() => {
notification.style.opacity = "1";
notification.style.transform = "translateY(0px)";
},100);
setTimeout(() => {
notification.style.opacity = "0";
notification.style.transform = "translateY(-20px)";
},2000);
setTimeout(() => {
notification.remove();
},2500);
}
/* HERO BUTTON SCROLL */
const heroBtn = document.querySelector(".hero-text button");
heroBtn.addEventListener("click", () => {
window.scrollTo({
top:900,
behavior:"smooth"
});
});
/* SEARCH BUTTON */
const searchBtn = document.querySelector(".search-box button");
searchBtn.addEventListener("click", () => {
const searchInput =
document.querySelector(".search-box input").value;
if(searchInput.trim() === ""){
alert("Please enter a product name");
}
else{
alert(
"Searching for: " + searchInput
);
}
});
/* CATEGORY HOVER EFFECT */
const categories =
document.querySelectorAll(".category");
categories.forEach(category => {
category.addEventListener("mouseenter", () => {
category.style.background = "#2563eb";
category.style.color = "white";
category.querySelector("i").style.color = "white";
});
category.addEventListener("mouseleave", () => {
category.style.background = "white";
category.style.color = "#111827";
category.querySelector("i").style.color = "#2563eb";
});
});
/* CARD BUTTON ANIMATION */
const cardButtons =
document.querySelectorAll(".card button");
cardButtons.forEach(button => {
button.addEventListener("click", () => {
button.innerText = "Added ✓";
button.style.background = "#16a34a";
setTimeout(() => {
button.innerText = "Add to Cart";
button.style.background = "#2563eb";
},1200);
});
});
/* STICKY NAVBAR SHADOW */
window.addEventListener("scroll", () => {
const navbar =
document.querySelector(".navbar");
if(window.scrollY > 20){
navbar.style.boxShadow =
"0 4px 20px rgba(0,0,0,0.08)";
}
else{
navbar.style.boxShadow =
"0 2px 10px rgba(0,0,0,0.05)";
}
});