-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
109 lines (72 loc) · 2.42 KB
/
main.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
const sliderImages = Array.from(document.querySelectorAll(".slider-container img"));
const slidesCount = sliderImages.length;
let currentSlide = 1;
const slideNumberElement = document.querySelector("#slide-number");
const nextButton = document.querySelector("#next");
const prevButton = document.querySelector("#prev");
const paginationElement = document.createElement("ul");
paginationElement.setAttribute("id", "pagination-ul");
for (let i = 1; i <= slidesCount; i++) {
let paginationItem = document.createElement("li");
paginationItem.setAttribute("data-index", `${i}`);
paginationItem.appendChild(document.createTextNode(`${i}`));
paginationElement.appendChild(paginationItem);
}
// Add The Created UL Element to The Page
document.querySelector("#indicators").appendChild(paginationElement);
// Get The New Created UL
let paginationCreatedUl = document.getElementById('pagination-ul');
// Get Pagination Items | Array.form [ES6 Feature]
let paginationsBullets = Array.from(document.querySelectorAll('#pagination-ul li'));
// Loop Through All Bullets Items
for (let i = 0; i < paginationsBullets.length; i++) {
paginationsBullets[i].onclick = function () {
currentSlide = parseInt(this.getAttribute('data-index'));
theChecker();
}
}
// Trigger The Checker Function
theChecker();
// Next Slide Function
function nextSlide() {
if (nextButton.classList.contains('disabled')) {
return false;
} else {
currentSlide++;
theChecker();
}
}
function prevSlide() {
if (prevButton.classList.contains('disabled')) {
return false;
} else {
currentSlide--;
theChecker();
}
}
nextButton.onclick = nextSlide;
prevButton.onclick = prevSlide;
function theChecker() {
slideNumberElement.textContent = 'Slide #' + (currentSlide) + ' of ' + (slidesCount);
removeAllActive();
sliderImages[currentSlide - 1].classList.add('active');
paginationCreatedUl.children[currentSlide - 1].classList.add('active');
if (currentSlide === 1) {
prevButton.classList.add('disabled');
} else {
prevButton.classList.remove('disabled');
}
if (currentSlide === slidesCount) {
nextButton.classList.add('disabled');
} else {
nextButton.classList.remove('disabled');
}
}
function removeAllActive() {
sliderImages.forEach(function (img) {
img.classList.remove('active');
});
paginationsBullets.forEach(function (bullet) {
bullet.classList.remove('active');
});
}