-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
103 lines (82 loc) · 3.24 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
lucide.createIcons();
const sidebar = document.querySelector(".sidebar");
const drag = document.querySelector(".sidebar .drag");
const content = document.querySelector(".content");
const images = document.querySelectorAll(".image");
const icon = sidebar.querySelector(".arrow");
const arrowUp = document.querySelector(".arrow-up");
const arrowDown = document.querySelector(".arrow-down");
const chevronsSelector = "[data-lucide^='chevrons-']";
const chevrons = document.querySelector(chevronsSelector);
const products = ["images/product1.png", "images/product2.png", "images/product3.png"];
const productNames = ["Matte Lipstick", "Velvet Blush", "Porcelain Foundation"];
let visibilityPercentage;
let currentIndex = 0;
drag.addEventListener("touchstart", startDrag);
drag.addEventListener("pointerdown", startDrag);
document.addEventListener("touchend", handleIcons);
document.addEventListener("pointerup", handleIcons);
function handleIcons(event) {
if (visibilityPercentage > 95) {
drag.innerHTML = '<i data-lucide="chevrons-left"></i>';
} else if (visibilityPercentage < 5) {
drag.innerHTML = '<i data-lucide="chevrons-right"></i>';
}
lucide.createIcons();
const attribute = event.target.getAttribute("data-lucide");
if (attribute === "chevron-up") {
currentIndex = (currentIndex - 1 + products.length) % products.length;
updateImage(); // Update the displayed image
}
if (attribute === "chevron-down") {
currentIndex = (currentIndex + 1) % products.length;
updateImage();
}
stopDrag(event);
}
function animation() {
gsap.to(chevronsSelector, {
x: "+=4", // Increase position to the right by 10 units
repeat: 3, // Repeat indefinitely
yoyo: true, // Reverse the animation to return to the initial position
ease: "power1.inOut", // Use a smooth easing function
duration: 0.3, // Duration of each animation cycle
});
}
animation();
function startDrag(event) {
document.addEventListener("pointermove", handleMove);
document.addEventListener("touchmove", handleMove);
//console.log("touched");
}
function stopDrag() {
animation();
document.removeEventListener("pointermove", handleMove);
document.removeEventListener("touchmove", handleMove);
//console.log("Stop touching");
}
function handleMove(event) {
//event.preventDefault();
const width = sidebar.clientWidth;
let x;
if (event.touches && event.touches.length > 0) {
// For mobile devices, use touch event coordinates
x = event.touches[0].clientX + width;
} else {
// For desktop, use mouse event coordinates
x = event.clientX;
}
// Calculate the percentage of sidebar visibility based on pointer position
visibilityPercentage = Math.max(0, Math.min(100, ((x - width) / width) * 100));
// Calculate the translateX value to move the sidebar
//const translateValue = `${-85 + (visibilityPercentage / 100) * 85}%`;
const translateValue = `${-85 + (visibilityPercentage / 100) * 85}%`;
// Set the sidebar's position accordingly
sidebar.style.transform = `translateX(${translateValue})`;
}
function updateImage() {
const image = document.querySelector(".image");
const paragraph = document.querySelector(".productname");
image.src = products[currentIndex];
paragraph.textContent = productNames[currentIndex];
}