-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
114 lines (93 loc) · 3.52 KB
/
main.js
File metadata and controls
114 lines (93 loc) · 3.52 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
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
/*==============================
= JavaScript From Scratch - JS
= File: main.js
==============================*/
/*==============================
= Scroll-based Animations
==============================*/
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
observer.unobserve(entry.target);
}
});
}, {
threshold: 0.1
});
document.querySelectorAll('.fade__scale, .fade__left, .fade__right, .fade__bottom').forEach(el => observer.observe(el));
/*==============================
= Menu de navegacion movil
==============================*/
const menuBtn = document.getElementById('header-menu-btn');
const closeBtn = document.getElementById('header-menu-close');
const menu = document.getElementById('header-menu');
const overlay = document.getElementById('menu-overlay');
// Elementos del menú
function openMenu() {
menu.classList.add('open');
document.body.classList.add('menu-open');
}
// Función para abrir el menú
function closeMenu() {
menu.classList.remove('open');
document.body.classList.remove('menu-open');
}
// Eventos para abrir/cerrar el menú
menuBtn.addEventListener('click', openMenu);
closeBtn.addEventListener('click', closeMenu);
overlay.addEventListener('click', closeMenu);
/*==============================
= ScrollSpy y enlaces activos
==============================*/
document.addEventListener("DOMContentLoaded", () => {
const links = document.querySelectorAll('#header-menu-list a');
const sectionIds = Array.from(links)
.map(link => link.getAttribute('href'))
.filter(href => href && href.startsWith('#'))
.map(href => href.slice(1));
const sections = sectionIds
.map(id => document.getElementById(id))
.filter(Boolean);
let isClickScrolling = false;
let clickTimeout = null;
// Observer para deteObserver to detect the visible sectionctar la sección visible
const observer = new IntersectionObserver((entries) => {
if (isClickScrolling) return; // If you are scrolling by clicking, do not change
let maxRatio = 0;
let visibleId = null;
entries.forEach(entry => {
if (entry.isIntersecting && entry.intersectionRatio > maxRatio) {
maxRatio = entry.intersectionRatio;
visibleId = entry.target.id;
}
});
if (visibleId) {
links.forEach(link => {
link.classList.toggle('active', link.getAttribute('href') === `#${visibleId}`);
});
}
}, {
threshold: [0.5, 0.7, 1.0]
});
sections.forEach(section => observer.observe(section));
// When clicked, only that link remains active during the animated scroll.
links.forEach(link => {
link.addEventListener('click', function (e) {
const targetId = this.getAttribute('href').slice(1);
const targetSection = document.getElementById(targetId);
if (targetSection) {
e.preventDefault();
isClickScrolling = true;
links.forEach(l => l.classList.remove('active'));
this.classList.add('active');
targetSection.scrollIntoView({ behavior: 'smooth' });
closeMenu();
clearTimeout(clickTimeout);
clickTimeout = setTimeout(() => {
isClickScrolling = false;
}, 600);
}
});
});
});