-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
144 lines (126 loc) · 5.24 KB
/
script.js
File metadata and controls
144 lines (126 loc) · 5.24 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
document.addEventListener('DOMContentLoaded', function () {
// ---------- Criar container de fundo adicional ----------
const backgroundContainer = document.createElement('div');
backgroundContainer.id = 'background-container';
document.body.insertBefore(backgroundContainer, document.body.firstChild);
// ---------- Mobile menu + smooth scroll ----------
const menuBtn = document.getElementById('menu-btn');
const mobileMenu = document.getElementById('mobile-menu');
const header = document.querySelector('header');
// Toggle mobile menu
menuBtn.addEventListener('click', () => {
const expanded = menuBtn.getAttribute('aria-expanded') === 'true';
menuBtn.setAttribute('aria-expanded', String(!expanded));
menuBtn.classList.toggle('open');
mobileMenu.classList.toggle('show');
});
// Close mobile menu when clicking any internal link and perform smooth scroll
const allNavLinks = document.querySelectorAll('a[href^="#"]');
allNavLinks.forEach(link => {
link.addEventListener('click', (e) => {
const href = link.getAttribute('href');
if (href && href.startsWith('#')) {
const target = document.querySelector(href);
if (target) {
e.preventDefault();
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
}
// close mobile menu if open
if (mobileMenu.classList.contains('show')) {
mobileMenu.classList.remove('show');
menuBtn.classList.remove('open');
menuBtn.setAttribute('aria-expanded', 'false');
}
});
});
// ---------- Top button ----------
const topBtn = document.getElementById('top-btn');
window.addEventListener('scroll', () => {
if (window.scrollY > 220) topBtn.classList.remove('hidden');
else topBtn.classList.add('hidden');
});
topBtn.addEventListener('click', () => window.scrollTo({ top: 0, behavior: 'smooth' }));
// ---------- Close mobile menu when clicking outside ----------
document.addEventListener('click', (e) => {
if (!header.contains(e.target) && mobileMenu.classList.contains('show')) {
mobileMenu.classList.remove('show');
menuBtn.classList.remove('open');
menuBtn.setAttribute('aria-expanded', 'false');
}
});
// ---------- Canvas: estrelas simples ----------
(function () {
const canvas = document.getElementById('stars');
const ctx = canvas.getContext('2d');
let w = canvas.width = window.innerWidth;
let h = canvas.height = window.innerHeight;
let stars = [];
// Garantir que o canvas sempre cubra toda a viewport
function updateCanvasSize() {
w = canvas.width = window.innerWidth;
h = canvas.height = window.innerHeight;
initStars();
}
function initStars() {
stars = [];
const count = Math.round((w * h) / 8000); // scale number of stars
for (let i = 0; i < count; i++) {
stars.push({
x: Math.random() * w,
y: Math.random() * h,
r: Math.random() * 1.6,
color: Math.random() > 0.7 ? '#a855f7' : '#ffffff',
speed: Math.random() * 0.4 + 0.02
});
}
}
function frame() {
ctx.clearRect(0, 0, w, h);
for (let s of stars) {
ctx.beginPath();
ctx.arc(s.x, s.y, s.r, 0, Math.PI * 2);
ctx.fillStyle = s.color;
ctx.fill();
s.y += s.speed;
if (s.y > h) s.y = 0;
}
requestAnimationFrame(frame);
}
// Atualizar o tamanho do canvas quando a janela for redimensionada
window.addEventListener('resize', updateCanvasSize);
// Atualizar o canvas quando a página rolar para ajustar a posição das estrelas
window.addEventListener('scroll', () => {
// Ajustar a posição das estrelas com base no scroll
const scrollY = window.scrollY;
stars.forEach(star => {
star.y += scrollY * 0.01; // Movimento sutil das estrelas durante o scroll
if (star.y > h) star.y = 0;
});
});
// Inicializar
updateCanvasSize();
frame();
})();
// ---------- Garantir que o fundo cubra toda a página ----------
function updateBackgroundHeight() {
const bodyHeight = document.body.scrollHeight;
const viewportHeight = window.innerHeight;
// Se o conteúdo for maior que a viewport, ajustar o fundo
if (bodyHeight > viewportHeight) {
document.getElementById('background-container').style.height = bodyHeight + 'px';
}
}
// Atualizar altura do fundo quando a janela for redimensionada
window.addEventListener('resize', updateBackgroundHeight);
// Atualizar altura do fundo quando o conteúdo mudar
const observer = new MutationObserver(updateBackgroundHeight);
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: false,
characterData: false
});
// Inicializar
updateBackgroundHeight();
});