-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
29 lines (24 loc) · 1.02 KB
/
script.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
// Function to handle smooth scrolling
function smoothScrollTo(targetSelector, offset = 0) {
const targetElement = document.querySelector(targetSelector);
if (targetElement) {
const targetPosition = targetElement.getBoundingClientRect().top + window.pageYOffset;
const startPosition = window.pageYOffset;
const distance = targetPosition - startPosition - offset;
window.scrollTo({
top: distance + startPosition,
behavior: 'smooth'
});
}
}
// Add smooth scrolling to all anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const targetId = this.getAttribute('href');
const headerOffset = document.querySelector('header').offsetHeight; // Assuming you have a header element
// Scroll to the target element with an offset
smoothScrollTo(targetId, headerOffset);
});
});
// Add any additional JavaScript you need for your page below