-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
86 lines (72 loc) · 2.85 KB
/
script.js
File metadata and controls
86 lines (72 loc) · 2.85 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
document.addEventListener('DOMContentLoaded', () => {
const track = document.querySelector('.carousel-track');
const nextButton = document.querySelector('.next-btn');
const prevButton = document.querySelector('.prev-btn');
const cards = Array.from(track.children);
let currentIndex = 0;
// Calcula el ancho de cada tarjeta más su margen
const getCardWidth = () => {
if (cards.length === 0) return 0;
const cardStyle = window.getComputedStyle(cards[0]);
const cardWidth = cards[0].offsetWidth;
const cardMarginRight = parseFloat(cardStyle.marginRight);
return cardWidth + cardMarginRight;
};
// Función principal para mover el carrusel
const updateTrack = () => {
const width = getCardWidth();
// Aplica la traducción (translateX) para mover el track
track.style.transform = `translateX(${-(width * currentIndex)}px)`;
// Actualiza el estado de los botones (deshabilitar en los extremos)
prevButton.disabled = currentIndex === 0;
nextButton.disabled = currentIndex === cards.length - 1;
};
// Navegación con botones
nextButton.addEventListener('click', () => {
if (currentIndex < cards.length - 1) {
currentIndex++;
updateTrack();
}
});
prevButton.addEventListener('click', () => {
if (currentIndex > 0) {
currentIndex--;
updateTrack();
}
});
// ----------------------------------------
// Lógica para SWIPE/Arrastre Táctil (Móviles)
// ----------------------------------------
let touchStartX = 0;
let touchEndX = 0;
// 1. Capturar el punto de inicio del toque
track.addEventListener('touchstart', (e) => {
touchStartX = e.changedTouches[0].screenX;
}, false);
// 2. Capturar el punto final del toque
track.addEventListener('touchend', (e) => {
touchEndX = e.changedTouches[0].screenX;
handleGesture();
}, false);
// 3. Procesar el gesto (swipe)
const handleGesture = () => {
const threshold = 50; // Mínima distancia para considerarlo un swipe
const difference = touchStartX - touchEndX;
if (difference > threshold) {
// Swipe a la izquierda (quiere ver el siguiente proyecto)
if (currentIndex < cards.length - 1) {
currentIndex++;
}
} else if (difference < -threshold) {
// Swipe a la derecha (quiere ver el proyecto anterior)
if (currentIndex > 0) {
currentIndex--;
}
}
updateTrack();
};
// Manejar el redimensionamiento de la ventana (para recalcular el ancho de la tarjeta)
window.addEventListener('resize', updateTrack);
// Inicializar la posición al cargar la página
updateTrack();
});