Skip to content

Commit 70ad326

Browse files
committed
added navbar js
1 parent 1e3b27f commit 70ad326

4 files changed

Lines changed: 58 additions & 8 deletions

File tree

src/assets/js/custom.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/assets/js/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Import Bootstrap JS
44
import * as bootstrap from 'bootstrap';
55
import './gsap.js';
6+
import './navbar.js';
67

78

89
// Import SCSS

src/assets/js/navbar.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
document.addEventListener('DOMContentLoaded', function () {
3+
const navLinks = document.querySelectorAll('.navbar-nav .nav-link');
4+
const sections = Array.from(navLinks)
5+
.map(link => document.querySelector(link.getAttribute('href')))
6+
.filter(Boolean);
7+
8+
const activateLink = (link) => {
9+
navLinks.forEach(item => {
10+
item.classList.remove('text-primary');
11+
item.classList.add('text-dark');
12+
});
13+
if (link) {
14+
link.classList.add('text-primary');
15+
link.classList.remove('text-dark');
16+
}
17+
};
18+
19+
navLinks.forEach(link => {
20+
link.addEventListener('mouseenter', () => {
21+
activateLink(link);
22+
});
23+
link.addEventListener('mouseleave', () => {
24+
const activeLink = document.querySelector('.navbar-nav .nav-link.active');
25+
activateLink(activeLink);
26+
});
27+
link.addEventListener('click', (event) => {
28+
navLinks.forEach(item => item.classList.remove('active'));
29+
link.classList.add('active');
30+
activateLink(link);
31+
});
32+
});
33+
34+
const onScroll = () => {
35+
const scrollPosition = window.scrollY + window.innerHeight / 3;
36+
let currentSection = null;
37+
38+
sections.forEach(section => {
39+
if (section.offsetTop <= scrollPosition && section.offsetTop + section.offsetHeight > scrollPosition) {
40+
currentSection = section;
41+
}
42+
});
43+
44+
if (currentSection) {
45+
const newActive = document.querySelector(`.navbar-nav .nav-link[href="#${currentSection.id}"]`);
46+
if (newActive) {
47+
navLinks.forEach(item => item.classList.remove('active'));
48+
newActive.classList.add('active');
49+
activateLink(newActive);
50+
}
51+
}
52+
};
53+
54+
window.addEventListener('scroll', onScroll);
55+
onScroll();
56+
});

src/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -973,8 +973,7 @@ <h3 class="text-white">Legal</h3>
973973

974974
<!-- Bootstrap JS -->
975975
<script src="./assets/js/main.js" type="module"></script>
976-
977-
976+
978977

979978
</body>
980979

0 commit comments

Comments
 (0)