AdSense Leaderboard

Google AdSense Placeholder

HEADER SLOT

Custom useScrollPosition Hook

Last updated:

Track the scroll position of the window or an element. Great for "Sticky" headers or "Scroll to Top" buttons.

The Code

typescript
import { useState, useEffect } from 'react';

export function useScrollPosition() {
  const [scrollPosition, setScrollPosition] = useState(0);

  useEffect(() => {
    const updatePosition = () => {
      setScrollPosition(window.scrollY);
    };
    
    window.addEventListener('scroll', updatePosition);
    updatePosition();
    
    return () => window.removeEventListener('scroll', updatePosition);
  }, []);

  return scrollPosition;
}

Pro Tip: Combine this with useThrottle for better performance on scroll-heavy pages.

Usage

typescript
function Navbar() {
  const scrollY = useScrollPosition();
  const isScrolled = scrollY > 50;

  return (
    <nav className={isScrolled ? 'bg-white shadow' : 'bg-transparent'}>
      Logo
    </nav>
  );
}
Sponsored Content

Google AdSense Placeholder

CONTENT SLOT

Sponsored

Google AdSense Placeholder

FOOTER SLOT