Smooth Scroll Changelog: v2.4 -> v3.3 - "The Big Refactor & Glow-Up" ✨
Alright folks, big jump from v2.4! This wasn't just dusting off the cobwebs; it's more like a full engine swap and a new paint job. Here's the scoop on what changed:
✨ New Stuff & Major Goodies:
- Motion Blur (Optional Bling!):
cfg.mBlur
is here! If you turn it on, the page gets a subtle (or not-so-subtle, depending on cfg.mBlurInt
) blur effect while scrolling fast. Looks slick, but might cost a tiny bit of performance on older machines. Off by default for max speed.
- Smarter Acceleration: Remember
scrollAcceleration
? It's been replaced with a more responsive system (cfg.accDelFct
, cfg.accMaxMult
). Now, quick consecutive scrolls build up a multiplier, making fast flicks feel way more punchy, up to a configurable limit. Feels more natural.
- VSync-Style Animation: Ditched the old refresh rate calculation (
targetRefreshRate
, requestAnimationFrameUpdate
). We're now using pure requestAnimationFrame
for the animation loop (_animStep
). This means the animation timing is directly tied to your display's refresh rate – smoother, less guesswork, and generally the modern standard.
🚀 Improvements & Refactors (Under the Hood):
- Core Animation Logic Overhaul: The entire scrolling calculation is simpler and based on a damping factor (
1 - cfg.smth
). Instead of complex time/ratio math, it's now just "move X% closer to the target each frame". More predictable, easier to tune.
- Better Target Finding (
_getTgt
): The logic for finding which element to scroll (the whole page? that nested div
?) is more robust. It uses composedPath
and directly checks scrollable dimensions and overflow
styles, handling edge cases better than the old isScrollable
/canScroll
/hasScrollbar
combo.
- Simplified State (
stMap
): No more juggling subPixelOffset
and pixelScrollAmount
. Each scrolling element now has a simple state object in a WeakMap
holding its target (tgtY
), current animated position (curY
), animation ID, timestamp for acceleration (ts
), and current acceleration multiplier (mult
). Cleaner!
- Modern Structure (IIFE): Wrapped the whole thing in an IIFE (Immediately Invoked Function Expression). Less global pollution compared to the old
SmoothScroll
object approach. Functions are mostly internal now (_
prefix).
- Event Handling Tweaks: Now uses
capture: true
for listeners to grab events early. Explicitly non-passive (passive: false
) for the wheel
event to reliably preventDefault()
. Also added touchstart
listener to cancel animation on touch devices.
- Performance Focus: By simplifying the animation math and state, and relying directly on
requestAnimationFrame
, the core loop should be lighter. Removed the need for refresh rate calculation overhead.
⚙️ Config Changes (Out with the Old, In with the New):
- Gone:
scrollSmoothness
, scrollAcceleration
, debugMode
, targetRefreshRate
, maxRefreshRate
, minRefreshRate
, animationDuration
, passiveEventListeners
.
- New:
-
smth
: The smoothness factor (0 to 1, higher is smoother, opposite of old scrollSmoothness
). Replaces scrollSmoothness
.
-
stpMult
: A simple overall multiplier for scroll distance.
-
accDelFct
, accMaxMult
: Control the new acceleration system. Replace scrollAcceleration
.
-
thrsh
: How close (in pixels) the scroll needs to be to the target before stopping. Replaces scrollThreshold
conceptually.
-
lnHt
: Default height (in pixels) for deltaMode === 1
(scroll by lines).
-
mBlur
, mBlurInt
: Toggle and control the motion blur intensity.
-
dbg
: Simple true/false toggle for debug console logs.
🗑️ Removed:
- The complex sub-pixel offset tracking logic. We round to the nearest pixel now.
- Dynamic refresh rate calculations and the associated min/max limits.
- The
SmoothScroll
global object and its public start
/stop
methods (it's all internal now).
- Browser fallbacks/polyfills (
requestAnimationFrame
, String.prototype.encamel
) - assuming modern browser support.
TL;DR:
Version 3.3 is a significant rewrite focusing on smoother animation via requestAnimationFrame
, a better acceleration feel, optional motion blur, more reliable target finding, and cleaner, modern code. Configuration is different, so check the new cfg
object! Should feel better overall. Enjoy the scroll! 🚀