-
Notifications
You must be signed in to change notification settings - Fork 0
/
carousel.js
71 lines (50 loc) · 1.99 KB
/
carousel.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
const track = document.getElementById("image-track");
const handleOnDown = e => track.dataset.mouseDownAt = e.clientX;
const handleOnUp = () => {
track.dataset.mouseDownAt = "0";
track.dataset.prevPercentage = track.dataset.percentage;
if (track.dataset.prevPercentage > (-100/6)) {
track.dataset.percentage = 0;
track.dataset.prevPercentage = track.dataset.percentage;
}
if ((track.dataset.percentage > (-300/6)) && (track.dataset.percentage < (-100/6))) {
track.dataset.percentage = (-100/3);
track.dataset.prevPercentage = track.dataset.percentage;
}
if ((track.dataset.percentage < (-300/6))) {
track.dataset.percentage = (-200/3);
track.dataset.prevPercentage = track.dataset.percentage;
}
track.animate({
transform: `translate(${track.dataset.prevPercentage}%, 0%)`
}, { duration: 1000, fill: "forwards", easing: 'ease'});
}
const handleOnMove = e => {
if(track.dataset.mouseDownAt === "0") return;
const mouseDelta = parseFloat(track.dataset.mouseDownAt) - e.clientX,
maxDelta = window.innerWidth / 2;
const percentage = ((mouseDelta / maxDelta) * -100),
nextPercentage = parseFloat(track.dataset.prevPercentage) + percentage;
if (nextPercentage > 0) {
nextPercentage = 0;
}
if (nextPercentage < (-200/3)) {
nextPercentage = -200/3;
}
track.dataset.percentage = nextPercentage;
track.animate({
transform: `translate(${nextPercentage}%, 0%)`
}, { duration: 1000, fill: "forwards" });
for(const image of track.getElementsByClassName("project")) {
image.animate({
backgroundPosition: `${100 + (nextPercentage)}% center`
}, { duration: 1000, fill: "forwards" });
}
}
/* -- Had to add extra lines for touch events -- */
track.onmousedown = e => handleOnDown(e);
track.ontouchstart = e => handleOnDown(e.touches[0]);
track.onmouseup = e => handleOnUp(e);
track.ontouchend = e => handleOnUp(e.touches[0]);
track.onmousemove = e => handleOnMove(e);
track.ontouchmove = e => handleOnMove(e.touches[0]);