-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
61 lines (52 loc) · 1.59 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
const slideContent = document.getElementsByClassName('slide_content');
const prev = document.getElementById('prev');
const next = document.getElementById('next');
const sliderRail = document.getElementById('slider_rail');
let currentPosition = 0;
let currentSlideIndex = 0;
let timer;
const totalSlideCount = sliderRail.children.length;
const slideWidth = 600;
const autoPlayInterval = 2000;
sliderRail.style.transform = 'translate3d(' + currentPosition + 'px, 0px, 0px)';
sliderRail.style.width = slideWidth * totalSlideCount + 'px';
function goNext() {
if (currentSlideIndex === totalSlideCount - 1) {
currentPosition = 0;
sliderRail.style.transform = 'translate3d(' + currentPosition + 'px, 0px, 0px)';
currentSlideIndex = 0;
}
else {
currentSlideIndex += 1;
currentPosition += -slideWidth;
sliderRail.style.transform = 'translate3d(' + currentPosition + 'px, 0px, 0px)';
}
}
function goPrev() {
if (currentSlideIndex === 0) {
currentPosition = -(slideWidth * (totalSlideCount - 1));
sliderRail.style.transform = 'translate3d(' + currentPosition + 'px, 0px, 0px)';
currentSlideIndex = totalSlideCount - 1;
}
else {
currentSlideIndex -= 1;
currentPosition += slideWidth;
sliderRail.style.transform = 'translate3d(' + currentPosition + 'px, 0px, 0px)';
}
}
function setTimer() {
timer = setInterval(() => {
goNext();
}, autoPlayInterval);
}
setTimer();
next.addEventListener('click', function() {
clearInterval(timer);
goNext();
setTimer();
});
prev.addEventListener('click', function() {
clearInterval(timer);
goPrev();
setTimer();
});