A powerful React hook for advanced scroll tracking, control, and animations. Perfect for creating scroll-based interactions, progress indicators, parallax effects, and intelligent scroll snapping.
- ๐ฏ Precise Scroll Tracking - Track scroll position, percentage, and direction
- ๐ฎ Smooth Scroll Control - Programmatically control scroll with smooth animations
- ๐ Custom Range Support - Define custom scroll ranges for specific elements
- ๐ Direction Detection - Detect scroll direction (up/down/none)
- ๐ฑ Responsive Design - Automatically adapts to window resize
- ๐จ Animation Ready - Perfect for scroll-based animations and effects
- ๐ TypeScript Support - Full TypeScript support with type definitions
npm install advanced-scroll-hook
import { useScroll } from 'advanced-scroll-hook'
function MyComponent() {
const { scrollData, setScroll } = useScroll()
return (
<div>
<div>Scroll: {scrollData.percent * 100}%</div>
<button onClick={() => setScroll(0, true)}>
Go to Top
</button>
</div>
)
}
Returns an object with scroll data and control functions.
interface UseScrollOptions {
container?: HTMLElement | null // Custom scroll container (default: window)
startHeight?: number // Start position in pixels (default: 0)
endHeight?: number // End position in pixels (default: document height)
}
interface UseScrollReturn {
scrollData: {
scrollY: number // Current scroll position in pixels
percent: number // Scroll percentage (0-1, can exceed bounds)
direction: 'up' | 'down' | 'none' // Scroll direction
isScrolling: boolean // Whether currently scrolling
}
percentToScrollHeight: (percent: number) => number // Convert percent to pixels
setScroll: (position: number, smooth?: boolean) => void // Scroll to position
}
import { useScroll } from 'advanced-scroll-hook'
function ScrollProgress() {
const { scrollData } = useScroll()
return (
<div className="progress-bar">
<div
className="progress-fill"
style={{ width: `${scrollData.percent * 100}%` }}
/>
</div>
)
}
Track scroll progress for a specific element within the viewport:
// Calculate scroll range for the target element
const calculateScrollRange = useCallback(() => {
const rect = keyDivRef.current.getBoundingClientRect()
const keyDivTop = rect.top + window.scrollY
return {
startHeight: keyDivTop, // When div top hits screen top
endHeight: keyDivTop + rect.height - window.innerHeight
}
}, [])
const { scrollData } = useScroll({
startHeight: scrollRange.startHeight,
endHeight: scrollRange.endHeight,
})
// Animate based on scroll progress
<div style={{
transform: `scale(${0.8 + scrollData.percent * 0.4})`,
opacity: scrollData.percent < 0 || scrollData.percent > 1 ? 0.3 : 1
}}>
Content scales as you scroll!
</div>
Create a three-page experience with scroll-based navigation:
// Calculate current page based on scroll percentage
const getCurrentPage = (percent: number) => {
if (percent < 0.3333) return 1
if (percent < 0.6666) return 2
return 3
}
// Jump to specific page
const goToPage = (page: number) => {
const targetPercent = page === 1 ? 0 : page === 2 ? 0.5 : 1
const targetHeight = percentToScrollHeight(targetPercent)
setScroll(targetHeight, true)
}
// Navigation buttons
{[1, 2, 3].map((page) => (
<button onClick={() => goToPage(page)}>
{page}
</button>
))}
Create intelligent directional scroll snapping with threshold-based page navigation:
// Smart snapping logic with 15% threshold
const handleSmartSnap = useCallback(() => {
const snapPoints = [0, 0.5, 1]
const distanceFromLast = Math.abs(scrollData.percent - lastSnapPositionRef.current)
const threshold = 0.15
if (distanceFromLast < threshold) return
let targetSnap = null
if (scrollData.direction === 'down') {
targetSnap = snapPoints.find(point => point > lastSnapPositionRef.current)
} else if (scrollData.direction === 'up') {
targetSnap = snapPoints.reverse().find(point => point < lastSnapPositionRef.current)
}
if (targetSnap !== null) {
setScroll(percentToScrollHeight(targetSnap), true)
lastSnapPositionRef.current = targetSnap
}
}, [scrollData.percent, scrollData.direction])
// Trigger snapping when scroll stops
useEffect(() => {
if (!scrollData.isScrolling) {
setTimeout(handleSmartSnap, 50)
}
}, [scrollData.isScrolling, handleSmartSnap])
- Reading Progress Indicators - Show how much of an article has been read
- Parallax Scrolling Effects - Create depth and motion in your designs
- Scroll-Based Animations - Trigger animations based on scroll position
- Section Navigation - Highlight current section in navigation
- Infinite Scroll - Load more content as user scrolls
- Scroll Snapping - Create smooth page-like scrolling experiences
- Custom Scroll Behaviors - Implement unique scroll interactions
# Install dependencies
npm install
# Start demo development server
npm run dev
# Build library
npm run build:lib
# Build demo
npm run build:demo
# Build both
npm run build
src/
โโโ lib/ # Library source
โ โโโ index.ts # Main export
โ โโโ useScroll.ts # Hook implementation
โโโ demo/ # Demo application
โโโ App.tsx # Demo app with examples
โโโ main.tsx
โโโ index.html
MIT ยฉ gaomeng1900