-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
158 lines (136 loc) · 4.71 KB
/
script.js
File metadata and controls
158 lines (136 loc) · 4.71 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
function initPage(){
initYear();
initMobileNav();
initHeaderShadow();
initScrollSpy();
initRevealOnScroll();
initToTop();
}
function initYear(){
const y = document.getElementById('year');
if (y) y.textContent = new Date().getFullYear();
}
// atualiza a variável CSS com a altura do header
function setHeaderHeightVar(){
const header = document.querySelector('.site-header');
const h = header?.offsetHeight || 64;
document.documentElement.style.setProperty('--header-h', `${h}px`);
}
function initMobileNav(){
const toggle = document.querySelector('.nav-toggle');
const nav = document.getElementById('site-nav');
if (!toggle || !nav) return;
const closeNav = () => {
nav.classList.remove('open');
toggle.setAttribute('aria-expanded', 'false');
document.documentElement.classList.remove('nav-open');
};
const openNav = () => {
setHeaderHeightVar();
nav.classList.add('open');
toggle.setAttribute('aria-expanded', 'true');
document.documentElement.classList.add('nav-open');
};
toggle.addEventListener('click', () => {
const isOpen = nav.classList.contains('open');
if (isOpen) closeNav(); else openNav();
});
// fecha ao clicar em um link interno
nav.querySelectorAll('a[href^="#"]').forEach(a =>
a.addEventListener('click', () => {
closeNav();
})
);
// fecha com ESC
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') closeNav();
});
// fecha ao clicar fora
document.addEventListener('click', (e) => {
if (!nav.classList.contains('open')) return;
const t = e.target;
if (!nav.contains(t) && !toggle.contains(t)) closeNav();
}, true);
// garante cálculo correto ao girar tela
window.addEventListener('orientationchange', () => {
setTimeout(() => {
setHeaderHeightVar();
closeNav();
}, 200);
});
}
function initHeaderShadow(){
const header = document.querySelector('.site-header');
const onScroll = () => {
const scrolled = window.scrollY > 8;
header?.classList.toggle('scrolled', scrolled);
};
onScroll();
document.addEventListener('scroll', onScroll, { passive: true });
// altura dinâmica do header (carregamento, resize, mudança de layout)
setHeaderHeightVar();
window.addEventListener('resize', () => setHeaderHeightVar());
if (document.fonts?.ready) document.fonts.ready.then(() => setHeaderHeightVar());
if (window.ResizeObserver && header){
const ro = new ResizeObserver(() => setHeaderHeightVar());
ro.observe(header);
}
// Ensure sections account for sticky header offset
document.querySelectorAll('section[id]').forEach(sec => {
sec.style.scrollMarginTop = (header?.offsetHeight || 72) + 'px';
});
}
function initScrollSpy(){
const links = Array.from(document.querySelectorAll('.site-nav .nav-link'));
const ids = links.map(l => l.getAttribute('href')).filter(Boolean).map(h => h.replace('#',''));
const sections = ids.map(id => document.getElementById(id)).filter(Boolean);
const activate = (id) => {
links.forEach(l => l.classList.toggle('active', l.getAttribute('href') === `#${id}`));
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting){
activate(entry.target.id);
}
});
}, { rootMargin: "-40% 0px -50% 0px", threshold: [0, 0.2, 0.5, 1] });
sections.forEach(sec => observer.observe(sec));
// Smooth scroll for internal links (native behavior + focus)
links.forEach(link => {
link.addEventListener('click', (e) => {
const href = link.getAttribute('href');
if (!href?.startsWith('#')) return;
const target = document.querySelector(href);
if (target){
e.preventDefault();
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
target.setAttribute('tabindex', '-1');
target.focus({ preventScroll: true });
setTimeout(() => target.removeAttribute('tabindex'), 500);
}
});
});
}
function initRevealOnScroll(){
const els = document.querySelectorAll('.will-reveal');
const io = new IntersectionObserver((entries, obs) => {
entries.forEach(e => {
if (e.isIntersecting){
e.target.classList.add('revealed');
obs.unobserve(e.target);
}
});
}, { threshold: 0.2, rootMargin: '0px 0px -40px 0px' });
els.forEach(el => io.observe(el));
}
function initToTop(){
const btn = document.getElementById('toTop');
if (!btn) return;
const toggle = () => btn.classList.toggle('show', window.scrollY > 400);
toggle();
document.addEventListener('scroll', toggle, { passive: true });
btn.addEventListener('click', () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
}
document.addEventListener('DOMContentLoaded', initPage);