top of page
bottom of page
// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const targetId = this.getAttribute('href');
// Recalculate targetElement inside the handler ensures it exists at click time
const targetElement = document.querySelector(targetId);
if (targetElement) {
const headerOffset = 80; // Adjust this value based on your header height
const elementPosition = targetElement.getBoundingClientRect().top;
const offsetPosition = elementPosition + window.pageYOffset - headerOffset;
window.scrollTo({
top: offsetPosition,
behavior: "smooth"
});
} else {
console.log("Scroll target not found:", targetId); // Add logging if needed
}
});
});