forked from ASEAN-Build-The-Earth/AseanBTE-Website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slidebar.js
108 lines (85 loc) · 2.47 KB
/
slidebar.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
const slider = document.querySelector(".slider");
const nextBtn = document.querySelector(".next-btn");
const prevBtn = document.querySelector(".prev-btn");
const slides = document.querySelectorAll(".slide");
const slideIcons = document.querySelectorAll(".slide-icon");
const numberOfSlides = slides.length;
var slideNumber = 0;
//image slider next button
nextBtn.addEventListener("click", () => {
slides.forEach((slide) => {
slide.classList.remove("active");
});
slideIcons.forEach((slideIcon) => {
slideIcon.classList.remove("active");
});
slideNumber++;
if (slideNumber > numberOfSlides - 1) {
slideNumber = 0;
}
slides[slideNumber].classList.add("active");
slideIcons[slideNumber].classList.add("active");
});
//image slider previous button
prevBtn.addEventListener("click", () => {
slides.forEach((slide) => {
slide.classList.remove("active");
});
slideIcons.forEach((slideIcon) => {
slideIcon.classList.remove("active");
});
slideNumber--;
if (slideNumber < 0) {
slideNumber = numberOfSlides - 1;
}
slides[slideNumber].classList.add("active");
slideIcons[slideNumber].classList.add("active");
});
//image slider autoplay
var playSlider;
var repeater = () => {
playSlider = setInterval(function () {
slides.forEach((slide) => {
slide.classList.remove("active");
});
slideIcons.forEach((slideIcon) => {
slideIcon.classList.remove("active");
});
slideNumber++;
if (slideNumber > numberOfSlides - 1) {
slideNumber = 0;
}
slides[slideNumber].classList.add("active");
slideIcons[slideNumber].classList.add("active");
}, 4000);
};
repeater();
//stop the image slider autoplay on mouseover
slider.addEventListener("mouseover", () => {
clearInterval(playSlider);
});
//start the image slider autoplay again on mouseout
slider.addEventListener("mouseout", () => {
repeater();
});
// Fade away effect
var header = document.getElementById("header");
function fadeOutOnScroll(element) {
if (!element) {
return;
}
var distanceToTop = window.pageYOffset + element.getBoundingClientRect().top;
var elementHeight = element.offsetHeight;
var scrollTop = document.documentElement.scrollTop;
var opacity = 1;
if (scrollTop > distanceToTop) {
opacity = 1 - (scrollTop - distanceToTop) / elementHeight;
}
if (opacity >= 0) {
element.style.opacity = opacity;
}
}
function scrollHandler() {
fadeOutOnScroll(header);
}
window.addEventListener("scroll", scrollHandler);