Overview
This module provides:- Smooth color interpolation based on scroll position
- Automatic CSS custom property updates
- Performance-optimized with
requestAnimationFrame - Responsive to scroll progress (0% to 100%)
Installation
The color transition is automatically initialized when the DOM is ready:Functions
initColorTransition()
Initializes the progressive color transition effect on scroll. Location:src/scripts/color-transition.js:2
Usage:
Initial color (red) at scroll position 0%Hex:
#ad0000Final color (orange) at scroll position 100%RGB:
rgb(227, 114, 1)- Calculates scroll progress as a percentage (0 to 1)
- Interpolates RGB values linearly between start and end colors
- Updates the
--color-primaryCSS custom property - Uses
requestAnimationFramefor optimized performance
Helper Functions
lerp()
Linear interpolation function for smooth color transitions. Location:src/scripts/color-transition.js:9
Signature:
Starting value (e.g., red channel value)
Ending value (e.g., red channel value)
Progress value between 0 and 1
number - Interpolated value rounded to nearest integer
Example:
updateColor()
Calculates and applies the color based on current scroll position. Location:src/scripts/color-transition.js:13
Internal Function
Calculation:
- Calculates total scrollable height
- Determines scroll progress (clamped to max 1.0)
- Interpolates each RGB channel
- Constructs RGB color string
- Updates CSS custom property on root element
handleScroll()
Throttle scroll events usingrequestAnimationFrame.
Location: src/scripts/color-transition.js:27
Internal Function
Optimization:
updateColor is only called once per frame, preventing excessive calculations during rapid scroll events.
CSS Integration
The module updates the--color-primary CSS custom property, which should be defined in your CSS:
Event Listeners
The module attaches a passive scroll listener for optimal performance:Indicates the listener will never call
preventDefault(), allowing the browser to optimize scrolling performanceInitialization
The module auto-initializes on DOM ready:updateColor() immediately after setup to ensure the correct color is set on page load.
Example Usage
Basic Implementation
Custom Color Scheme
To modify the color scheme, edit the source file:Multiple Color Properties
Extend the function to update multiple properties:Scroll Progress Calculation
The module calculates scroll progress as:scrollHeight= total document height minus viewport height (maximum scrollable distance)scrollProgress= current scroll position divided by maximum scroll (clamped to 1.0)
| Scroll Position | Document Height | Viewport Height | Progress |
|---|---|---|---|
| 0px | 3000px | 1000px | 0.0 (0%) |
| 1000px | 3000px | 1000px | 0.5 (50%) |
| 2000px | 3000px | 1000px | 1.0 (100%) |
| 2500px | 3000px | 1000px | 1.0 (clamped) |
Performance Considerations
Optimizations:- Uses
requestAnimationFrameto sync with browser paint cycles - Passive scroll listener for improved scroll performance
- Ticking flag prevents redundant frame requests
- Minimal DOM manipulation (single CSS property update)
- ~60 FPS on modern browsers
- Negligible CPU impact (less than 1% on modern hardware)
- No layout thrashing (only CSS custom property update)
Browser Support
Required Features:- CSS Custom Properties (CSS Variables)
requestAnimationFrame- Passive event listeners (gracefully degrades)
- Chrome/Edge 49+
- Firefox 31+
- Safari 9.1+
- Opera 36+
Export
The module exports the initialization function:Debugging
To debug color transitions, add logging toupdateColor():