-
Notifications
You must be signed in to change notification settings - Fork 0
/
index2.js
51 lines (42 loc) · 1.44 KB
/
index2.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
document.addEventListener("DOMContentLoaded", function() {
let mainPosts = document.querySelectorAll(".main-post");
let posts = document.querySelectorAll(".post");
if (!posts.length || !mainPosts.length) {
console.error("Required elements are missing in the DOM");
return;
}
let i = 0;
let postIndex = 0;
let currentPost = posts[postIndex];
let currentMainPost = mainPosts[postIndex];
let progressInterval = setInterval(progress, 100); // 180
function progress() {
if (i === 100) {
i = -5;
// reset progress bar
currentPost.querySelector(".progress-bar__fill").style.width = 0;
document.querySelector(
".progress-bar--primary .progress-bar__fill"
).style.width = 0;
currentPost.classList.remove("post--active");
postIndex++;
currentMainPost.classList.add("main-post--not-active");
currentMainPost.classList.remove("main-post--active");
// reset postIndex to loop over the slides again
if (postIndex === posts.length) {
postIndex = 0;
}
currentPost = posts[postIndex];
currentMainPost = mainPosts[postIndex];
} else {
i++;
currentPost.querySelector(".progress-bar__fill").style.width = `${i}%`;
document.querySelector(
".progress-bar--primary .progress-bar__fill"
).style.width = `${i}%`;
currentPost.classList.add("post--active");
currentMainPost.classList.add("main-post--active");
currentMainPost.classList.remove("main-post--not-active");
}
}
});