Sha256: 42da520fed74766efb91b1e54030383e04a3f3ae42fa0b9a1a9abc1481895177
Contents?: true
Size: 1.72 KB
Versions: 7
Compression:
Stored size: 1.72 KB
Contents
// scroll-navigation.js document.addEventListener("DOMContentLoaded", function () { let scrollThreshold = 200; // Pixels scrolled before triggering scroll to the next section let scrollTimeout; let lastScrollTop = 0; // Get all section elements const sections = Array.from(document.querySelectorAll("section")); // Function to scroll smoothly to a specific section function scrollToSection(section) { if (section) { window.scrollTo({ top: section.offsetTop, behavior: "smooth", }); } } // Function to handle scrolling behavior function onScroll() { const scrollTop = window.scrollY; if (scrollTop - lastScrollTop > scrollThreshold) { // User has scrolled down past the threshold let currentIndex = sections.findIndex((section) => { return section.getBoundingClientRect().top + window.scrollY > scrollTop; }); if (currentIndex > 0) { // Scroll to the next section scrollToSection(sections[currentIndex]); } lastScrollTop = scrollTop; } else if (lastScrollTop - scrollTop > scrollThreshold) { // User has scrolled up past the threshold let currentIndex = sections.findIndex((section) => { return ( section.getBoundingClientRect().top + window.scrollY <= scrollTop ); }); if (currentIndex > 0) { // Scroll to the previous section scrollToSection(sections[currentIndex - 1]); } lastScrollTop = scrollTop; } // Reset timeout to prevent excessive function calls clearTimeout(scrollTimeout); scrollTimeout = setTimeout(() => { lastScrollTop = window.scrollY; }, 100); } window.addEventListener("scroll", onScroll); });
Version data entries
7 entries across 7 versions & 1 rubygems