-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
71 lines (61 loc) · 2.58 KB
/
script.js
File metadata and controls
71 lines (61 loc) · 2.58 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
import { images } from './images.js'
const main = document.querySelector('main')
const MAX_FRAMES = 147
let currentFrame = 0
// init with the first image
const img = document.createElement('img')
// append img to main
img.className = 'scroll-image'
main.appendChild(img)
//verificacion
function updateImage(frame = 0) {
const src = images?.[frame]?.p;
if (src) {
img.src = src;
} else {
console.error('No se pudo obtener la imagen para frame:', frame);
}
}
updateImage(currentFrame)
// Altura máxima del scroll
let maxScroll = document.documentElement.scrollHeight - window.innerHeight;
let lastFrameUpdate = 0
window.addEventListener('resize', () => {
maxScroll = document.documentElement.scrollHeight - window.innerHeight;
});
window.addEventListener('scroll', () => {
if (Date.now() - lastFrameUpdate < 1) return true
// Actualizamos el contador
lastFrameUpdate = Date.now()
// Posición actual del scroll
const scrollPosition = window.scrollY
// Calcular el porcentaje del scroll
const scrollFraction = scrollPosition / maxScroll;
// ¿Qué frame le toca?
const frame = Math.floor(scrollFraction * MAX_FRAMES)
// nos evitemos algo de trabajo cuando
// al hacer scroll, el frame que le toca es el mismo
// que ya tenía
if (currentFrame !== frame) {
// creamos el id del nombre del archivo
updateImage(frame)
currentFrame = frame
}
// HEADER: Desaparecer gradualmente
const header = document.querySelector('header');
const headerDisappearDistance = window.innerHeight * 0.5; // Desaparece en 50% de la pantalla
const headerOpacity = Math.max(0, 1 - (scrollPosition / headerDisappearDistance));
header.style.opacity = headerOpacity;
// FOOTER: Aparecer solo al final
const footer = document.querySelector('footer');
const scrollPercentage = (scrollPosition / maxScroll) * 100;
// Debug - para verificar si funciona
console.log('Scroll %:', scrollPercentage.toFixed(1), 'Footer opacity:', footer.style.opacity);
// Aparece cuando está a 80% del scroll máximo (reducido para testing)
if (scrollPercentage >= 80) {
const fadeInProgress = Math.min(1, (scrollPercentage - 80) / 20); // De 80% a 100%
footer.style.opacity = fadeInProgress;
} else {
footer.style.opacity = 0;
}
});