-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavbar Module.code.javascript
More file actions
59 lines (52 loc) · 1.78 KB
/
Navbar Module.code.javascript
File metadata and controls
59 lines (52 loc) · 1.78 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
// js/modules/navbar.js
export const initNavbar = () => {
const navbar = document.querySelector('.navbar');
const menuToggle = document.querySelector('.menu-toggle');
const navLinks = document.querySelector('.nav-links');
let lastScroll = 0;
// Mobile menu toggle
menuToggle?.addEventListener('click', () => {
menuToggle.classList.toggle('active');
navLinks.classList.toggle('active');
document.body.classList.toggle('menu-open');
});
// Navbar scroll behavior
window.addEventListener('scroll', () => {
const currentScroll = window.pageYOffset;
// Add background when scrolling
if (currentScroll > 50) {
navbar.classList.add('scrolled');
} else {
navbar.classList.remove('scrolled');
}
// Hide/show navbar on scroll
if (currentScroll <= 0) {
navbar.classList.remove('scroll-up');
return;
}
if (currentScroll > lastScroll && !navbar.classList.contains('scroll-down')) {
navbar.classList.remove('scroll-up');
navbar.classList.add('scroll-down');
} else if (currentScroll < lastScroll && navbar.classList.contains('scroll-down')) {
navbar.classList.remove('scroll-down');
navbar.classList.add('scroll-up');
}
lastScroll = currentScroll;
});
// Smooth scroll for navigation links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
menuToggle.classList.remove('active');
navLinks.classList.remove('active');
document.body.classList.remove('menu-open');
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
};