Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
62 lines (54 loc) · 2.05 KB
/
Copy pathscript.js
File metadata and controls
62 lines (54 loc) · 2.05 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
// Debounce function to limit how often a function can be called
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
document.addEventListener("DOMContentLoaded", () => {
console.log("Page loaded successfully.");
// Scroll to top functionality
const scrollTopButton = document.getElementById('scrollTop');
const header = document.querySelector('header');
// Debounced scroll handler
const handleScroll = debounce(() => {
// Toggle visibility of back-to-top button
if (scrollTopButton && window.scrollY > 300) {
scrollTopButton.classList.add('visible');
} else if (scrollTopButton) {
scrollTopButton.classList.remove('visible');
}
// Toggle compact header styling
if (header && window.scrollY > 40) {
header.classList.add('scrolled');
} else if (header) {
header.classList.remove('scrolled');
}
}, 100);
// Show/hide scroll button based on scroll position
window.addEventListener('scroll', handleScroll);
// Scroll to top when button is clicked
if (scrollTopButton) {
const scrollToTop = () => window.scrollTo({ top: 0, behavior: 'smooth' });
scrollTopButton.addEventListener('click', scrollToTop);
scrollTopButton.addEventListener('keydown', (event) => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
scrollToTop();
}
});
}
// Add active state to navigation links
const navLinks = document.querySelectorAll('.nav-left a');
const currentLocation = window.location.href;
navLinks.forEach(link => {
if (link.href === currentLocation) {
link.classList.add('active');
}
});
});