-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
190 lines (166 loc) · 5.85 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
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
let countEl = document.querySelector('.count')
let countPlus = document.querySelector('.countPlus')
let countMinus = document.querySelector('.countMinus')
let addCartBtn = document.querySelector('.addCartBtn')
let cartCountEl = document.querySelector('.cart-count')
let cartBtn = document.querySelector('.cartBtn')
let totalPriceEl = document.querySelector('.total-price')
let totalAmountEl = document.querySelector('.total-amount')
let cartContainer = document.querySelector('.cart-container')
let cartBox = document.querySelector('.cartBox')
let imgBtnsParent = document.querySelector('.imgBtns')
let mainImg = document.querySelector('.main-img img')
let emptyCartTxt = document.querySelector('.emptyCartTxt')
let deleteCartItem = document.querySelector('.deleteCartItem')
let lightBox = document.querySelector('.lightBox-container')
let closeLightBox = document.querySelector('.close-lightBox')
let prevBtn = document.querySelector('.prevBtn')
let nextBtn = document.querySelector('.nextBtn')
let mainPrevBtn = document.querySelector('.mainPrevBtn')
let mainNextBtn = document.querySelector('.mainNextBtn')
let galleryImg = document.querySelector('.gallery img')
let galleryCtrl = document.querySelector('.gallery-control')
const toggleBtn = document.querySelector('.sidebar-toggle')
const closeBtn = document.querySelector('.close-btn')
const sidebar = document.querySelector('.sidebar-container')
let count = 0;
let cartCount = 0;
let price = 125;
let total = 0;
let currentImg = 0;
let imgArr =
['./images/image-product-1.jpg',
'./images/image-product-2.jpg',
'./images/image-product-3.jpg',
'./images/image-product-4.jpg']
// add click event on plus btn to plus the count
countPlus.addEventListener('click', function(){
count++;
countEl.innerHTML = count
})
// add click event on minus btn to minus the count
countMinus.addEventListener('click', function(){
count--;
countEl.innerHTML = count
if (count < 0) {
count = 0
countEl.innerHTML = count
}
})
// add click event on addCartBtn to show the nums of cart items in cartContainer
addCartBtn.addEventListener('click', function() {
if (count > 0) {
cartCountEl.classList.add('showCartCount')
cartCount = cartCount + count
cartCountEl.innerHTML = cartCount
}
if (cartCount > 0) {
count = 0
countEl.innerHTML = count
}
totalPriceEl.innerHTML = `$${price}.00 x ${cartCount} `
totalAmountEl.textContent = `$${cartCount * price}.00`
if (cartCount > 0) {
cartBox.classList.add('showCartBox')
emptyCartTxt.classList.remove('showEmptyCartTxt')
}
})
// add click event to toggle cartContainer
cartBtn.addEventListener('click', function() {
cartContainer.classList.toggle('showCartContainer')
emptyCartTxt.classList.add('showEmptyCartTxt')
if (cartBox.classList.contains('showCartBox')) {
emptyCartTxt.classList.remove('showEmptyCartTxt')
}
})
// set mainImg and galleryImg as first src of imgArr
window.addEventListener('DOMContentLoaded', function() {
mainImg.src = imgArr[0]
galleryImg.src = imgArr[0]
//crating imgbtns for mainImg
for (let i = 0; i < imgArr.length; i++) {
let childEl = document.createElement('img')
childEl.src = imgArr[i]
childEl.className = 'imgBtn'
childEl.alt = 'product image'
childEl.onclick = () => {
mainImg.src = childEl.src
}
imgBtnsParent.appendChild(childEl)
}
//creating img control btns for lightbox's main image
for (let i = 0; i < imgArr.length; i++) {
let childEl = document.createElement('img')
childEl.src = imgArr[i]
childEl.className = 'gallery-btn'
childEl.alt = 'product image'
childEl.onclick = () => {
galleryImg.src = childEl.src
}
galleryCtrl.appendChild(childEl)
}
})
//add click event to delete btn to delete the cart item
deleteCartItem.addEventListener('click', function() {
cartBox.classList.remove('showCartBox')
emptyCartTxt.classList.add('showEmptyCartTxt')
cartCount = 0
cartCountEl.classList.remove('showCartCount')
//cartCountEl.innerHTML = cartCount
})
//add click event on main-Img to open light box
mainImg.addEventListener('click', function() {
lightBox.classList.add('showLightBoxContainer')
})
// add click event on close btn to close the lightbox
closeLightBox.addEventListener('click', function() {
lightBox.classList.remove('showLightBoxContainer')
})
// show img based on imgArr
function showImg(img) {
galleryImg.src = imgArr[img];
}
// show next img for gallery image
nextBtn.addEventListener("click", function (e) {
console.log(e.target)
currentImg++;
if (currentImg > imgArr.length - 1) {
currentImg = 0;
}
showImg(currentImg)
});
// show prev img for gallery image
prevBtn.addEventListener("click", function (e) {
console.log(e.target)
currentImg--;
if (currentImg < 0) {
currentImg = imgArr.length - 1;
}
showImg(currentImg)
});
//show mainImg based on imgArr
function showMainImg(img) {
mainImg.src = imgArr[img]
}
//show next image for mainImg
mainNextBtn.addEventListener("click", function () {
currentImg++;
if (currentImg > imgArr.length - 1) {
currentImg = 0;
}
showMainImg(currentImg)
});
//show prev image for mainImg
mainPrevBtn.addEventListener("click", function () {
currentImg--;
if (currentImg < 0) {
currentImg = imgArr.length - 1;
}
showMainImg(currentImg)
});
toggleBtn.addEventListener('click', function() {
sidebar.classList.add('show-sidebar')
})
closeBtn.addEventListener('click', function() {
sidebar.classList.remove('show-sidebar')
})