-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
44 lines (39 loc) · 1.06 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
const testimonials = document.querySelectorAll(".testimonial");
const nextBtn = document.querySelectorAll(".next");
const preBtn = document.querySelectorAll(".prev");
testimonials.forEach((item, i) => {
item.style.transform = `translateX(${100 * (i - 0)}%)`;
});
const maxLength = testimonials.length;
let curVal = 0;
function moveNext() {
if (curVal === maxLength - 1) curVal = 0;
else {
curVal++;
}
moveSlide(curVal);
}
nextBtn.forEach((i) => {
i.addEventListener("click", moveNext);
});
function movePrev() {
if (curVal === 0) curVal = maxLength - 1;
else {
curVal--;
}
moveSlide(curVal);
}
preBtn.forEach((i) => {
i.addEventListener("click", movePrev);
});
function moveSlide(curIndex) {
testimonials.forEach((item, i) => {
item.style.transform = `translateX(${100 * (i - curIndex)}%)`;
item.style.transition = "0.5s";
});
}
document.addEventListener("keydown", function (e) {
console.log(e.key);
if (e.key === "ArrowRight") moveNext();
if (e.key === "ArrowLeft") movePrev();
});