-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
134 lines (131 loc) · 4.74 KB
/
app.js
File metadata and controls
134 lines (131 loc) · 4.74 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
const menuBtn = document.getElementById("menu-btn");
const closeBtn = document.querySelector(".close-btn");
const menu = document.querySelector(".nav_links");
const overlay = document.querySelector(".overlay");
const mainThumbnail = document.querySelector(".main-thumbnail");
const mainThumbnailLightBox = document.querySelector(
".lightbox-container .main-thumbnail"
);
const images = document.querySelectorAll(".preview img");
const plusBtn = document.querySelector("#plus");
const minusBtn = document.querySelector("#minus");
const amount = document.querySelector(".amount");
const nextBtn = document.getElementById("next");
const prevBtn = document.getElementById("previous");
const slider = document.querySelector(".mobile-thumb");
const thumbMob = document.querySelector(".thumb-mob");
const cartBtn = document.querySelector(".cart-btn");
const cart = document.querySelector(".cart-wrp");
const closeLightboxBtn = document.querySelector(".close-lightbox");
const lightbox = document.querySelector(".lightbox");
const addBtn = document.querySelector(".add_btn");
const indicator = document.querySelector(".indicator");
const wrp = document.querySelector(".cart-content");
let amountValue = 0;
let currentImg = 1;
indicator.style.display = "none";
function openMenu() {
menu.classList.add("active");
overlay.classList.add("active");
}
function closeMenu() {
menu.classList.remove("active");
overlay.classList.remove("active");
}
function handlePlus() {
amountValue++;
amount.innerText = amountValue;
}
function handleMinus() {
if (amountValue > 0) {
amountValue--;
}
amount.innerText = amountValue;
}
function nextImage() {
if (currentImg == 4) {
currentImg = 1;
} else {
currentImg++;
}
thumbMob.src = `./images/image-product-${currentImg}.jpg`;
}
function prevImage() {
if (currentImg == 1) {
currentImg = 4;
} else {
currentImg--;
}
thumbMob.src = `./images/image-product-${currentImg}.jpg`;
}
function toggleCart() {
cart.classList.toggle("invisible");
}
function closeLightBox() {
lightbox.classList.add("invisible");
}
function openLightBox() {
lightbox.classList.remove("invisible");
}
function addItem() {
if (amountValue > 0) {
const total = 125.00 * amountValue;
wrp.classList.remove("empty");
wrp.innerHTML = `<div class="product">
<div>
<img src="./images/image-product-1-thumbnail.jpg" class="product-img" alt="product">
<div class="product-info">
<p class="product-title">Fall Limited Edition Sneakers</p>
<p><span>$125.00</span> × <span class="number">${amountValue}</span> <b>$${total}</b></p>
</div>
<button class="delete-btn" onclick="deleteItem()"><img src="./images/icon-delete.svg" alt="delete"></button>
</div>
<button class="checkout-btn">Checkout</button>
</div>`;
indicator.style.display = "block";
indicator.innerText = amountValue;
}
}
function deleteItem() {
wrp.classList.add("empty");
wrp.innerHTML = `<p>Your cart is empty</p>`;
indicator.style.display = "none";
}
images.forEach((image) => {
image.addEventListener("click", () => {
const lastImg = document.querySelectorAll(".selected");
if (lastImg) {
lastImg[0].classList.remove("selected");
}
image.classList.add("selected");
const selectedImg = document.querySelector(".selected");
switch (selectedImg.getAttribute("src")) {
case "./images/image-product-1-thumbnail.jpg":
mainThumbnail.src = "./images/image-product-1.jpg";
mainThumbnailLightBox.src = "./images/image-product-1.jpg";
break;
case "./images/image-product-2-thumbnail.jpg":
mainThumbnail.src = "./images/image-product-2.jpg";
mainThumbnailLightBox.src = "./images/image-product-2.jpg";
break;
case "./images/image-product-3-thumbnail.jpg":
mainThumbnail.src = "./images/image-product-3.jpg";
mainThumbnailLightBox.src = "./images/image-product-3.jpg";
break;
case "./images/image-product-4-thumbnail.jpg":
mainThumbnail.src = "./images/image-product-4.jpg";
mainThumbnailLightBox.src = "./images/image-product-4.jpg";
break;
}
});
});
menuBtn.addEventListener("click", openMenu);
closeBtn.addEventListener("click", closeMenu);
plusBtn.addEventListener("click", handlePlus);
minusBtn.addEventListener("click", handleMinus);
nextBtn.addEventListener("click", nextImage);
prevBtn.addEventListener("click", prevImage);
cartBtn.addEventListener("click", toggleCart);
closeLightboxBtn.addEventListener("click", closeLightBox);
mainThumbnail.addEventListener("click", openLightBox);
addBtn.addEventListener("click", addItem);