// Blog page smooth scrolling functionality let smoothScrollHandlers = new Map(); function handleSmoothScroll(event) { const link = event.currentTarget; const href = link.getAttribute('href'); // Only handle anchor links that start with # if (!href || !href.startsWith('#')) { return; } // Prevent default navigation behavior event.preventDefault(); event.stopPropagation(); const targetId = href.substring(1); // Remove the # const targetElement = document.getElementById(targetId); if (targetElement) { // Calculate offset for fixed header const headerElement = document.querySelector('#global-header'); const headerHeight = headerElement ? headerElement.offsetHeight : 80; const offsetPosition = targetElement.offsetTop - headerHeight - 20; // Smooth scroll to the target with proper offset window.scrollTo({ top: offsetPosition, behavior: 'smooth' }); } } export function init() { // Find all anchor links on the page that start with # const anchorLinks = document.querySelectorAll('a[href^="#"]'); anchorLinks.forEach(link => { // Create a bound handler for this specific link const boundHandler = handleSmoothScroll.bind(null); smoothScrollHandlers.set(link, boundHandler); // Add the event listener link.addEventListener('click', boundHandler); }); console.log(`Blog page: Added smooth scroll handlers to ${anchorLinks.length} anchor links`); } export function teardown() { // Remove all event listeners smoothScrollHandlers.forEach((handler, link) => { link.removeEventListener('click', handler); }); // Clear the handlers map smoothScrollHandlers.clear(); console.log('Blog page: Removed all smooth scroll handlers'); }