-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
181 lines (144 loc) · 4.98 KB
/
index.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
//cart filling part
let count=0;
function Increase(){
count+=1;
document.getElementById('Number').innerHTML=count;
}
function Decrease(){
if(count>0)
{
count-=1;
document.getElementById('Number').innerHTML=count;
}
else document.getElementById('Number').innerHTML=0;
}
function ADD(){
let val = document.getElementById('not').innerHTML;
count+=+val;
if(count>0){
document.getElementById('not').innerHTML=count;
document.getElementById('not').style.display='block';
}
else{
document.getElementById('not').style.display='none';
document.getElementById('cartFilled').style.display='none';
}
}
function ShowCart(){
if(count==0){
document.getElementById('cart').style.display='block';
document.getElementById('cartEmpty').style.display='block';
}
else{
document.getElementById('cartEmpty').style.display='none';
document.getElementById('cart').style.display='block';
document.getElementById('cartFilled').style.display='flex';
document.getElementById('Qte').innerHTML='$125.00 * '+count;
document.getElementById('finalPrice').innerHTML='$'+125.00*count;
}
}
document.addEventListener('mouseup', function(e) {
var container = document.getElementById('cart');
if (!container.contains(e.target)) {
container.style.display = 'none';
}
});
function deleteFromCart(){
count--;
if(count>0){
document.getElementById('not').innerHTML=count;
document.getElementById('Number').innerHTML=count;
document.getElementById('Qte').innerHTML='$125.00 * '+count;
document.getElementById('finalPrice').innerHTML='$'+125.00*count;
}
else{
document.getElementById('not').style.display='none';
document.getElementById('cartFilled').style.display='none';
document.getElementById('cartEmpty').style.display='block';
document.getElementById('Number').innerHTML=0;
}
}
// scrolling picture part
let picScroll = document.querySelectorAll('.pic'); // return array
let heroImg= document.querySelector('.main-pic');
picScroll.forEach(img => {
img.addEventListener('click', onThumbClick);
});
function onThumbClick(event) {
//clear active state for all thumbnails
picScroll.forEach(img => {
img.classList.remove('active');
});
//set active thumbnail
event.target.parentElement.classList.add('active');
//update hero image
document.getElementById('main-pic').src = event.target.src.replace('-thumbnail', '');
}
// gestion des lightbox
const picsScroll = document.querySelectorAll('.pic-scroll');
const mainPic = document.querySelector('.main-pic-scrollPic');
picsScroll.forEach(img =>{
img.addEventListener('click', onClickScroll);
});
heroImg.addEventListener('click', dispScroll);
function onClickScroll(event) {
//clear active state for all thumbnails
picsScroll.forEach(img => {
img.classList.remove('active');
});
//set active thumbnail
event.target.parentElement.classList.add('active');
//update hero image
mainPic.src = event.target.src.replace('-thumbnail', '');
}
const close = document.querySelector('.close');
close.addEventListener('click', e=>{
document.querySelector('.lightBox').style.display='none';
});
function dispScroll(){
document.querySelector('.lightBox').style.display='block';
document.addEventListener('mouseup',function(e){
const scroll = document.querySelector('.pics-scroll');
if(!scroll.contains(e.target)){
document.querySelector('.lightBox').style.display='none';
}
});
}
// gestion de scrolling en utilisant les fleches next /previous
// put variables in array
const next = document.querySelector('.next');
const prev = document.querySelector('.previous');
const imagePrincipale = document.querySelector('.main-pic-scrollPic');
const picGallery = document.querySelectorAll(".pic-scroll");
next.addEventListener('click', handleNextBtn);
prev.addEventListener('click', handlePrevBtn);
function handleNextBtn() {
let imageIndex = getCurrentImageIndex();
imageIndex++;
if (imageIndex > 4) {
imageIndex = 1;
}
setPrincipaleImage(imageIndex);
}
function handlePrevBtn() {
let imageIndex = getCurrentImageIndex();
imageIndex--;
if (imageIndex < 1) {
imageIndex = 4;
}
setPrincipaleImage(imageIndex);
}
function getCurrentImageIndex() {
//pop() give us the last elements of the array after deleting it
const imageIndex = parseInt(imagePrincipale.src.split('\\').pop().split('/').pop().replace('.jpg', '').replace('image-product-', ''));
return imageIndex;
}
function setPrincipaleImage(imageIndex) {
imagePrincipale.src = `./images/image-product-${imageIndex}.jpg`;
//images are not sync
picGallery.forEach(img => {
img.classList.remove('active');
});
//set active thumbnail
picGallery[imageIndex-1].classList.add('active');
}