Smooth Scroll

The Smooth Scroll class enhances the user experience by implementing fluid, inertia-based scrolling behavior. This reduces the harsh jumps typically seen in default scroll actions, providing a seamless scrolling effect across the page. The system dynamically adjusts the scroll position based on user input, gradually easing to the target position.

Key Features:
  • Inertia-based Scrolling: Smoothly transitions to the target position, giving a more natural scrolling experience.
  • Wheel & Key Support: Captures mouse wheel, arrow keys, page up/down, home, and end keys for smooth movement.
  • Touch Support: Optionally enables smooth scrolling on touch devices by translating touch gestures into inertia scrolling.
  • Clamping: Prevents over-scrolling by limiting the target scroll to the page bounds.
  • Customizable: Fine-tune the easing effect, scroll threshold, and touch sensitivity using configurable options.
Options:
  • ease (default: 0.075) – Controls the scroll easing. Lower values result in slower easing.
  • threshold (default: 0.5) – Minimum difference required between current and target scroll to continue easing.
  • enableTouch (default: false) – Enables touch-based scrolling.
  • touchSensitivity (default: 2) – Adjusts the responsiveness of touch scroll. Higher values increase the movement.
Example Usage:
import SmoothScroll from './others/smooth_scroll/smooth_scroll.js'; const smoothScroll = new SmoothScroll({ ease: 0.08, threshold: 0.5, enableTouch: true, touchSensitivity: 2 });
How It Works:

Smooth Scroll listens for wheel, keyboard, and touch events. When triggered, it prevents the default behavior and calculates the target scroll position. The requestAnimationFrame loop incrementally adjusts the scroll, easing towards the target until the difference falls below the threshold. This ensures a polished and responsive interaction.

Destroying Smooth Scroll:

To disable Smooth Scroll and restore default scrolling, call the destroy() method. This removes all event listeners and cancels any active animation frames.

Example:
smoothScroll.destroy();
Benefits:
  • Improved user experience through fluid navigation.
  • Enhances accessibility by allowing natural scroll behavior on various input devices.
  • Supports both desktop and touch-based interactions for consistency across devices.
See example