/** * File back-to-top.js. * * Handles back to top button functionality. */ ( function() { const backToTopButton = document.getElementById( 'back-to-top' ); // Return early if button doesn't exist. if ( ! backToTopButton ) { return; } // Show/hide button based on scroll position function toggleBackToTop() { if ( window.pageYOffset > 300 ) { backToTopButton.style.display = 'flex'; } else { backToTopButton.style.display = 'none'; } } // Scroll to top when button is clicked backToTopButton.addEventListener( 'click', function( e ) { e.preventDefault(); window.scrollTo( { top: 0, behavior: 'smooth', } ); } ); // Listen for scroll events window.addEventListener( 'scroll', toggleBackToTop ); // Check initial scroll position toggleBackToTop(); }() );