-
-
Notifications
You must be signed in to change notification settings - Fork 570
/
Copy pathscript.js
76 lines (66 loc) · 2.16 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const slider = document.querySelector(".slider-container"),
slides = Array.from(document.querySelectorAll(".slide"));
let isDragging = false,
startPos = 0,
currentTranslate = 0,
prevTranslate = 0,
animationID = 0,
currentIndex = 0;
// Disable context menu
window.oncontextmenu = (event) => {
event.preventDefault();
event.stopPropagation();
return false;
};
const getPositionX = (event) =>
event.type.includes("mouse") ? event.pageX : event.touches[0].clientX;
const setSliderPosition = () => {
slider.style.transform = `translateX(${currentTranslate}px)`;
};
const animation = () => {
setSliderPosition();
if (isDragging) requestAnimationFrame(animation);
};
const setPositionByIndex = () => {
currentTranslate = currentIndex * -window.innerWidth;
prevTranslate = currentTranslate;
setSliderPosition();
};
const touchStart = (index) => {
return (event) => {
currentIndex = index;
startPos = getPositionX(event);
isDragging = true;
// https://css-tricks.com/using-requestanimationframe/
animationID = requestAnimationFrame(animation);
slider.classList.add("grabbing");
};
};
const touchEnd = () => {
isDragging = false;
cancelAnimationFrame(animationID);
const movedBy = currentTranslate - prevTranslate;
if (movedBy < -100 && currentIndex < slides.length - 1) currentIndex += 1;
if (movedBy > 100 && currentIndex > 0) currentIndex -= 1;
setPositionByIndex();
slider.classList.remove("grabbing");
};
const touchMove = (event) => {
if (isDragging) {
const currentPosition = getPositionX(event);
currentTranslate = prevTranslate + currentPosition - startPos;
}
};
slides.forEach((slide, index) => {
const slideImage = slide.querySelector("img");
slideImage.addEventListener("dragstart", (e) => e.preventDefault());
// Touch events
slide.addEventListener("touchstart", touchStart(index));
slide.addEventListener("touchend", touchEnd);
slide.addEventListener("touchmove", touchMove);
// Mouse events
slide.addEventListener("mousedown", touchStart(index));
slide.addEventListener("mouseup", touchEnd);
slide.addEventListener("mouseleave", touchEnd);
slide.addEventListener("mousemove", touchMove);
});