-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigator.js
60 lines (51 loc) · 2.01 KB
/
navigator.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
document.addEventListener('DOMContentLoaded', (event) => {
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const targetId = this.getAttribute('href');
const targetElement = document.querySelector(targetId);
if (targetElement) {
const headerOffset = 70; // Height of the header
const elementPosition = targetElement.offsetTop;
const offsetPosition = elementPosition - headerOffset;
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
}
});
});
});
document.addEventListener('DOMContentLoaded', () => {
if (window.innerWidth <= 768) {
const hamburger = document.getElementById('hamburger-btn');
const nav = document.querySelector('nav');
// Initially set or ensure the nav is hidden on page load within a mobile view
nav.style.display = nav.style.display || 'none';
hamburger.addEventListener('click', () => {
// Toggle the display state of the nav
nav.style.display = (nav.style.display === 'none') ? 'flex' : 'none';
});
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const targetId = this.getAttribute('href');
const targetElement = document.querySelector(targetId);
if (targetElement) {
const headerOffset = 70; // Height of the header
const elementPosition = targetElement.getBoundingClientRect().top + window.pageYOffset;
const offsetPosition = elementPosition - headerOffset;
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
// Close the nav menu when a link is clicked
nav.style.display = 'none';
}
});
});
}
});
document.getElementById("hamburger-btn").addEventListener("click", function() {
document.querySelector("nav").classList.toggle("show");
});