-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
executable file
·103 lines (80 loc) · 2.78 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const imgs = [
"./imgs/paint_fluid_art_stains_179700_3840x2400.jpg",
"./imgs/paint_fluid_art_stains_181136_3840x2400.jpg",
"./imgs/paint_fluid_art_stains_181255_3840x2400.jpg",
"./imgs/paint_liquid_fluid_art_174351_3840x2400.jpg",
"./imgs/paint_liquid_fluid_art_175385_3840x2400.jpg",
"./imgs/paint_liquid_fluid_art_177787_3840x2400.jpg",
"./imgs/paint_liquid_fluid_art_178075_3840x2400-1.jpg",
];
//main
const main = document.querySelector(".main");
//thumbnail
const track = document.getElementById("image-track");
// console.log(track);
const handleOnDown = (e) => (track.dataset.mouseDownAt = e.clientX);
const handleOnUp = () => {
track.dataset.mouseDownAt = "0";
track.dataset.prevPercentage = track.dataset.percentage;
};
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,
nextPercentageUnconstrained =
parseFloat(track.dataset.prevPercentage) + percentage,
nextPercentage = Math.max(Math.min(nextPercentageUnconstrained, 0), -100);
track.dataset.percentage = nextPercentage;
track.animate(
{
transform: `translate(${nextPercentage}%, -50%)`,
},
{ duration: 1200, fill: "forwards" }
);
for (const image of track.getElementsByClassName("thumb-image")) {
image.animate(
{
objectPosition: `${100 + nextPercentage}% center`,
},
{ duration: 1200, fill: "forwards" }
);
}
};
//track movements
window.onmousedown = (e) => handleOnDown(e);
window.ontouchstart = (e) => handleOnDown(e.touches[0]);
window.onmouseup = (e) => handleOnUp(e);
window.ontouchend = (e) => handleOnUp(e.touches[0]);
window.onmousemove = (e) => handleOnMove(e);
window.ontouchmove = (e) => handleOnMove(e.touches[0]);
//display images
for (let index = 0; index < imgs.length; index++) {
const currentImage = imgs[index];
const element = ` <img
class="main-image"
src="${currentImage}"
draggable="false"
/>`;
const thumbImage = ` <img
class="thumb-image"
src="${currentImage}"
draggable="false"
/>`;
main.innerHTML += element;
track.innerHTML += thumbImage;
}
//starting point
let currentIndex = 0;
const mainImages = document.getElementsByClassName("main-image");
const thumbImages = document.getElementsByClassName("thumb-image");
mainImages[currentIndex].classList.add("active");
//Thumbnail imgs nav
for (let index = 0; index < thumbImages.length; index++) {
const thisThumb = thumbImages[index];
thisThumb.addEventListener("click", function () {
mainImages[currentIndex].classList.remove("active");
currentIndex = index;
mainImages[currentIndex].classList.add("active");
});
}