You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Scroll management, locking, position tracking, and viewport detection.
Quick Start
import{ScrollUtils}from'@zappzarapp/browser-utils/scroll';// Smooth scroll to elementScrollUtils.scrollIntoView(element,{behavior: 'smooth'});// Lock scroll for modalconstunlock=ScrollUtils.lock();// ... modal open ...unlock();// Restore scrolling// Check if element is visibleif(ScrollUtils.isInViewport(element)){loadContent();}
functionopenModal(modal: HTMLElement): ()=>void{// Lock scroll and save unlock functionconstunlock=ScrollUtils.lock();modal.classList.add('open');return()=>{modal.classList.remove('open');unlock();// Restore scroll position and enable scrolling};}
Smooth Scroll Navigation
// Smooth scroll to anchordocument.querySelectorAll('a[href^="#"]').forEach((link)=>{link.addEventListener('click',(e)=>{e.preventDefault();constselector=link.getAttribute('href')!;ScrollUtils.scrollToElement(selector,{behavior: 'smooth',block: 'start',});});});
Back to Top Button
constbackToTop=document.getElementById('back-to-top')!;// Show/hide based on scroll positionconstcleanup=ScrollUtils.onScroll(()=>{const{ y }=ScrollUtils.getScrollPercentage();backToTop.hidden=y<20;},{throttle: 100});// Scroll to top on clickbackToTop.addEventListener('click',()=>{ScrollUtils.scrollToTop({behavior: 'smooth'});});
Lazy Loading
// Load content when element comes into viewfunctionlazyLoad(element: HTMLElement,loader: ()=>void): void{constcleanup=ScrollUtils.onScroll(()=>{if(ScrollUtils.isInViewport(element,0.1)){cleanup();loader();}},{throttle: 100,passive: true});}
constprogressBar=document.getElementById('progress')!;constcleanup=ScrollUtils.onScroll(()=>{const{ y }=ScrollUtils.getScrollPercentage();progressBar.style.width=`${y}%`;},{throttle: 50});
Scroll Position Preservation
// Save scroll position before navigationconstsavePosition=(): void=>{constpos=ScrollUtils.getScrollPosition();sessionStorage.setItem('scrollPos',JSON.stringify(pos));};// Restore scroll positionconstrestorePosition=(): void=>{constsaved=sessionStorage.getItem('scrollPos');if(saved){const{ x, y }=JSON.parse(saved);ScrollUtils.scrollTo({left: x,top: y});sessionStorage.removeItem('scrollPos');}};
Configuration
onScroll Options
Option
Type
Default
Description
throttle
number
0
Throttle delay in milliseconds
passive
boolean
true
Use passive event listener
onScrollDirection Options
Option
Type
Default
Description
threshold
number
10
Minimum scroll distance to detect direction
throttle
number
100
Throttle delay in milliseconds
Performance Considerations
Throttled Events - Use throttle option to limit callback frequency
Passive Listeners - Enabled by default for better scroll performance
Cleanup Functions - Always call cleanup to remove event listeners