-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
75 lines (64 loc) · 2.21 KB
/
script.js
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
// Smooth scrolling
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
document.querySelector(this.getAttribute('href')).scrollIntoView({
behavior: 'smooth'
});
});
});
// Active navigation link
const sections = document.querySelectorAll("section");
const navLinks = document.querySelectorAll("nav ul li a");
window.addEventListener("scroll", () => {
let current = "";
sections.forEach((section) => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (pageYOffset >= sectionTop - sectionHeight / 3) {
current = section.getAttribute("id");
}
});
navLinks.forEach((link) => {
link.classList.remove("active");
if (link.getAttribute("href").slice(1) === current) {
link.classList.add("active");
}
});
});
// Particle animation
function createParticles() {
const particlesContainer = document.getElementById('particles');
const particleCount = 50;
for (let i = 0; i < particleCount; i++) {
const particle = document.createElement('div');
particle.classList.add('particle');
particle.style.width = `${Math.random() * 5 + 1}px`;
particle.style.height = particle.style.width;
particle.style.left = `${Math.random() * 100}%`;
particle.style.top = `${Math.random() * 100}%`;
const animation = particle.animate([
{ transform: 'translate(0, 0)' },
{ transform: `translate(${Math.random() * 50 - 25}px, ${Math.random() * 50 - 25}px)` }
], {
duration: Math.random() * 3000 + 5000,
direction: 'alternate',
iterations: Infinity,
easing: 'ease-in-out'
});
particlesContainer.appendChild(particle);
}
}
createParticles();
// Mobile menu toggle
const menuToggle = document.querySelector('.menu-toggle');
const navUl = document.querySelector('nav ul');
menuToggle.addEventListener('click', () => {
navUl.classList.toggle('show');
});
// Close menu when a link is clicked
document.querySelectorAll('nav ul li a').forEach(link => {
link.addEventListener('click', () => {
navUl.classList.remove('show');
});
});