-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (116 loc) · 3.58 KB
/
index.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
let options = {
stickyImage: true, // follows the user's finger
staticImage: false, // stops on the screen when the user lets go, doesn't follow finger.
hideImage: false, // shows the image after the finger leaves the hotspot. If sticky, this option should stay true.
transition: "0.5s", // duration in seconds that it takes for the change from one image to another.
products: ["google.com", "instagram.com", "facebook.com", "youtube.com"],
};
lucide.createIcons();
const hotspots = Array.from(document.querySelectorAll(".grid .hotspot"));
const images = Array.from(document.querySelectorAll(".grid img"));
const cta = document.querySelector(".cta a");
const pointer = document.querySelector(".pointer");
let x, y;
let currentImage;
let animationComplete = false;
function animate() {
const timeline = gsap.timeline();
if (window.innerWidth < 600) {
// Mobile/tablet
timeline.to(pointer, {
x: "40vw",
duration: 1,
ease: "power1.inOut",
});
timeline.to(pointer, {
x: "-20vw",
y: "25vh",
duration: 1,
ease: "power1.inOut",
});
timeline.to(pointer, {
x: "40vw",
duration: 1,
ease: "power1.inOut",
});
timeline.to(pointer, {
opacity: 0,
duration: 2,
onComplete: function () {
pointer.style.display = "none";
animationComplete = true;
},
});
} else {
// Desktop
timeline.to(pointer, {
x: "16vw",
duration: 1,
ease: "power1.inOut",
});
timeline.to(pointer, {
x: 0,
y: "25vh",
duration: 1,
ease: "power1.inOut",
});
timeline.to(pointer, {
x: "16vw",
duration: 1,
ease: "power1.inOut",
});
timeline.to(pointer, {
opacity: 0,
duration: 2,
onComplete: function () {
pointer.style.display = "none";
animationComplete = true;
},
});
}
}
animate();
//if mobile animation should be different
// Loop over images and hide each one with opacity 0
images.forEach((image) => {
image.style.opacity = 0;
image.style.transition = `opacity ${options.transition}`;
});
function followFinger(event) {
event.preventDefault();
x = event.clientX; // Use clientX for mouse event or event.touches[0].clientX for touch event
y = event.clientY; // Use clientY for mouse event or event.touches[0].clientY for touch event
if (!animationComplete) return;
//.clientX //.layerX //.offsetX //.pageX //.screenX //.x
/*
const index = images.indexOf(currentImage);
const x = offset[index].x;
const y = offset[index].y;
*/
//const x = event.clientX / 4 - 200;
//const y = event.clientY / 2 - 200;
hotspots.forEach((hotspot, index) => {
const { left, right, top, bottom } = hotspot.getBoundingClientRect();
if (x >= left && x <= right && y >= top && y <= bottom) {
currentImage = images[index];
if (currentImage) currentImage.style.opacity = 1;
cta.href = `https://${options.products[index]}`;
}
});
//if (currentImage) currentImage.style.transform = `translate(${x}px, ${y}px)`; //calc()
}
// add mousemove (hover) event listener for desktop
document.addEventListener("pointermove", followFinger);
hotspots.forEach((hotspot, index) => {
/*
hotspot.addEventListener("pointerover", (event) => {
event.preventDefault();
currentImage = images[index];
if (currentImage) currentImage.style.opacity = 1;
});*/
hotspot.addEventListener("pointerout", (event) => {
event.preventDefault();
if (options.hideImage && currentImage) currentImage.style.opacity = 0;
cta.href = `https://${options.products[index]}`;
});
});