-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
83 lines (71 loc) · 2.09 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
const wrapper = document.querySelector('.wrapper');
const carousel = document.querySelector('.carousel');
const leftArrow = document.querySelector('.prev-btn');
const rightArrow = document.querySelector('.next-btn');
const images = document.querySelectorAll('img');
const pageButtons = document.querySelectorAll('.page-btn');
let imageIndex = 0;
let intervalId = null;
const INTERVAL_MILLISECONDS = 2000;
const FIRST_PAGE_INDEX = 0;
const LAST_PAGE_INDEX = images.length - 1;
const changeImagePage = () => {
carousel.style.transform = `translate(-${imageIndex * 100}%)`;
};
const toggleActiveDotStyle = () => {
// for (let i = 0; i < images.length; i++) {
// if (i === imageIndex) {
// pageButtons[i].classList.add('page-btn-active');
// } else {
// pageButtons[i].classList.remove('page-btn-active');
// }
// }
pageButtons.forEach((button, index) => {
if (index === imageIndex) {
button.classList.add('page-btn-active');
} else {
button.classList.remove('page-btn-active');
}
});
};
const increaseActiveImage = () => {
if (imageIndex === LAST_PAGE_INDEX) {
imageIndex = FIRST_PAGE_INDEX;
} else {
imageIndex++;
}
toggleActiveDotStyle();
changeImagePage();
};
const decreaseActiveImage = () => {
if (imageIndex === FIRST_PAGE_INDEX) {
imageIndex = LAST_PAGE_INDEX;
} else {
imageIndex--;
}
toggleActiveDotStyle();
changeImagePage();
};
const switchActiveImage = (activeDot) => {
imageIndex = activeDot;
toggleActiveDotStyle();
changeImagePage();
};
const autoSlideImage = () => {
intervalId = setInterval(() => {
increaseActiveImage();
}, INTERVAL_MILLISECONDS);
};
const pauseAutoSlideImage = () => {
clearInterval(intervalId);
};
pageButtons.forEach((pageBtn, pageIndex) => {
pageBtn.addEventListener('click', () => {
switchActiveImage(pageIndex);
});
});
wrapper.addEventListener('mouseover', pauseAutoSlideImage);
wrapper.addEventListener('mouseleave', autoSlideImage);
leftArrow.addEventListener('click', decreaseActiveImage);
rightArrow.addEventListener('click', increaseActiveImage);
autoSlideImage();