-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
76 lines (61 loc) · 2.42 KB
/
Copy pathscript.js
File metadata and controls
76 lines (61 loc) · 2.42 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
document.addEventListener('DOMContentLoaded', function () {
const sections = document.querySelectorAll('section');
const navLinks = document.querySelectorAll('nav a');
const menu = document.querySelector('.menu');
window.addEventListener('scroll', function () {
activateNavLinks();
animateOnScroll('.animate');
});
navLinks.forEach(function (navLink) {
navLink.addEventListener('click', function (event) {
event.preventDefault();
const targetId = this.getAttribute('href').substring(1);
const targetSection = document.getElementById(targetId);
scrollToSection(targetSection);
toggleMenu();
});
});
document.querySelector('.logo a').addEventListener('click', function (event) {
event.preventDefault();
scrollToSection(document.getElementById('home'));
});
document.querySelector('.menu-toggle').addEventListener('click', function () {
toggleMenu();
});
function activateNavLinks() {
let currentSectionId = '';
sections.forEach(function (section) {
const sectionTop = section.offsetTop - 50;
const sectionBottom = sectionTop + section.clientHeight;
if (window.scrollY >= sectionTop && window.scrollY <= sectionBottom) {
currentSectionId = section.id;
}
});
navLinks.forEach(function (navLink) {
navLink.classList.remove('active');
const targetId = navLink.getAttribute('href').substring(1);
if (targetId === currentSectionId) {
navLink.classList.add('active');
}
});
}
function scrollToSection(targetSection) {
window.scrollTo({
top: targetSection.offsetTop - 40,
behavior: 'smooth',
});
}
function animateOnScroll(animationClass) {
const elements = document.querySelectorAll(animationClass);
elements.forEach((element) => {
const position = element.getBoundingClientRect().top;
const windowHeight = window.innerHeight;
if (position < windowHeight / 1.3) {
element.classList.add('animate__animated', 'animate__fadeIn');
}
});
}
function toggleMenu() {
menu.classList.toggle('show');
}
});