-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
48 lines (38 loc) · 1.04 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
let carousel = document.querySelector('.carousel');
let stage = document.querySelector('.carousel-stage');
let prev = document.querySelector('.carousel-prev');
let next = document.querySelector('.carousel-next');
let slide = document.querySelectorAll('.single-slide');
let slidesNumber = slide.length - 1;
let currentIndex = 0;
let slideWidth = slide[0].clientWidth;
function goToSlide(index) {
if (index < 0) {
index = slidesNumber;
} else if (index > slidesNumber) {
index = 0;
}
carousel.style.left = index * (-slideWidth);
currentIndex = index;
}
function slideToNext() {
goToSlide(currentIndex + 1);
}
function slideToPrev() {
goToSlide(currentIndex - 1);
}
document.onkeydown = checkKey;
function checkKey(e) {
if (e.keyCode == '37') {
slideToPrev();
}
else if (e.keyCode == '39') {
slideToNext();
}
}
next.addEventListener('click', slideToNext);
prev.addEventListener('click', slideToPrev);
function autorotate() {
setInterval(slideToNext, 4000);
}
autorotate();