-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
77 lines (65 loc) · 2.97 KB
/
Copy pathscript.js
File metadata and controls
77 lines (65 loc) · 2.97 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
77
document.addEventListener('DOMContentLoaded', () => {
// Select all internal links starting with '#'
const navLinks = document.querySelectorAll('a[href^="#"]');
navLinks.forEach(link => {
link.addEventListener('click', function(e) {
const targetId = this.getAttribute('href');
let targetPosition = 0;
if (targetId !== '#' && targetId !== '') {
const targetElement = document.querySelector(targetId);
if (!targetElement) return;
const header = document.querySelector('.layout-body-header');
const headerHeight = header ? header.offsetHeight : 54;
targetPosition = targetElement.getBoundingClientRect().top + window.pageYOffset - headerHeight - 24;
}
e.preventDefault();
const startPosition = window.pageYOffset;
const distance = targetPosition - startPosition;
const duration = 600; // Snappier duration for fast response
let start = null;
// easeOutCubic: starts instantly at high speed, decelerates at the very end
function easeOutCubic(t, b, c, d) {
t /= d;
t--;
return c * (t * t * t + 1) + b;
}
function step(timestamp) {
if (!start) start = timestamp;
const progress = timestamp - start;
window.scrollTo(0, easeOutCubic(progress, startPosition, distance, duration));
if (progress < duration) {
window.requestAnimationFrame(step);
} else {
window.scrollTo(0, targetPosition);
}
}
window.requestAnimationFrame(step);
});
});
// Mobile menu toggle
const mobileToggle = document.querySelector('.mobile-menu-toggle');
const primaryNav = document.querySelector('.primary-nav');
if (mobileToggle && primaryNav) {
mobileToggle.addEventListener('click', () => {
primaryNav.classList.toggle('active');
const icon = mobileToggle.querySelector('i');
if (primaryNav.classList.contains('active')) {
icon.classList.remove('bx-menu');
icon.classList.add('bx-x');
} else {
icon.classList.remove('bx-x');
icon.classList.add('bx-menu');
}
});
// Close menu when a link is clicked
const navLinksToClose = primaryNav.querySelectorAll('.nav-link');
navLinksToClose.forEach(link => {
link.addEventListener('click', () => {
primaryNav.classList.remove('active');
const icon = mobileToggle.querySelector('i');
icon.classList.remove('bx-x');
icon.classList.add('bx-menu');
});
});
}
});