-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
65 lines (55 loc) · 1.63 KB
/
Copy pathscript.js
File metadata and controls
65 lines (55 loc) · 1.63 KB
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
const throttleFunction = (func, delay) => {
let prev = 0;
return (...args) => {
let now = Date.now();
if (now - prev > delay) {
prev = now;
func(...args);
}
};
};
const centerDiv = document.querySelector("#center");
const images = [
"https://images.unsplash.com/photo-1600812898697-679d14c1ad7d?w=600",
"https://images.unsplash.com/photo-1640975972263-1f73398e943b?w=600",
"https://images.unsplash.com/photo-1523381210434-271e8be1f52b?w=600",
"https://images.unsplash.com/photo-1558769132-cb1aea1f9565?w=600",
"https://images.unsplash.com/photo-1490481651871-ab68de25d43d?w=600"
];
centerDiv.addEventListener(
"mousemove",
throttleFunction((dets) => {
const rect = centerDiv.getBoundingClientRect();
const x = dets.clientX - rect.left;
const y = dets.clientY - rect.top;
// ✅ image pehle load hogi
const img = new Image();
img.src = images[Math.floor(Math.random() * images.length)];
img.onload = () => {
// ✅ div ONLY after image loaded
const div = document.createElement("div");
div.classList.add("imagediv");
div.style.left = x + "px";
div.style.top = y + "px";
div.appendChild(img);
centerDiv.appendChild(div);
// animation
gsap.fromTo(
img,
{ y: "100%" },
{ y: "0%", duration: 0.4, ease: Power1.easeOut }
);
gsap.to(img, {
y: "-100%",
delay: 0.5,
duration: 1,
ease: Power2.easeIn,
});
setTimeout(() => {
div.remove();
}, 1500);
};
// ❌ agar image fail ho → kuch bhi mat banao
img.onerror = () => {};
}, 400)
);