Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

Seven years of performance work on simpleParallax.js, up to v8

In 2019 I wrote a case study about simpleParallax.js, a library that adds a parallax effect directly on <img> tags instead of background-image. It ended with this sentence: "Performances are constant challenges against w

In 2019 I wrote a case study about simpleParallax.js, a library that adds a parallax effect directly on <img> tags instead of background-image. It ended with this sentence: "Performances are constant challenges against what has been done."

Seven years and four major versions later, here is what that challenge looked like, and how version 8 finally fixed a bug that had been open since 2020.

What didn't change

The core idea from 2019 is still there, with the same formulas:

  • the image is wrapped in a container with overflow: hidden, so it can move without breaking the layout;
  • the image is scaled up, so it has room to move without showing blank space:
range       = imageHeight * scale - imageHeight
progress    = (viewportBottom - imageTop) / (viewportHeight + imageHeight)
translation = progress * range - range / 2

The only visible change: the default scale went from 1.3 to 1.5 in 7.0, when the React and vanilla versions were aligned.

What changed

  • 6.0 (2024): React and TypeScript. The library became a React component first, with the vanilla version still available.
  • 6.2.1 (2025): no more useless re-renders. The React version was re-rendering while the parallax was animating.
  • 7.0 (2026): no re-render at all while scrolling. The transform is computed in the animation frame and written directly to the element's style, outside React's render cycle. The vanilla and React versions now share the same core, so they behave the same, and nothing touches the browser at import time, so it works with server-side rendering.
  • 8.0 (2026): a per-frame animation engine, which is the rest of this post.

The bug that survived every optimization

Since 2020 (issue #47), the effect stuttered on iPhone: badly in Safari, a bit less in Chrome iOS, which runs on the same WebKit engine. On a Mac it was smooth with a trackpad and jerky with a mouse wheel.

None of the fixes I tried worked:

  • delay: 0 (the default from 5.5.1): still jerky, sometimes worse;
  • the vanilla version instead of React: same result;
  • rounding the translation to a hundredth of a pixel: no change;
  • turning off the transition on Safari (6.3): not a real fix.

So I measured on an iPhone 16e. The page ran at 60 fps, and the library applied every scroll position it received. But JavaScript only received the scroll position 18 to 33 times per second, with jumps of 73 to 486 px. With a mouse wheel on a Mac, it's about 100 px per notch.

That was the cause. Until 7.x, the library copied those jumps as they came, and the CSS transition used for delay restarted on every new position. The problem wasn't Safari itself, it was how irregular the scroll input was. Why iOS reports the position so irregularly, maybe because scrolling is handled by the system, outside JavaScript, I couldn't verify.

The fix: let the scroll set a target, not the position

Version 8 removes the CSS transition entirely. The scroll only sets a target position, and on every animation frame, each image covers a fraction of the remaining distance:

value += (target - value) * (1 - Math.exp(-4 * dt / delay))

dt is the time since the last frame, so the motion is the same at 60 Hz or 120 Hz, and the image keeps moving smoothly however irregular the scroll is. With this constant, it covers about 98% of the way after delay seconds. Two other easings, spring and bounce, use damped springs instead.

Doing nothing when nothing moves

The engine also made the idle case free:

  • Vanilla: element positions are measured once, at init and on refresh(), then cached. A frame only reads the scroll position. If it hasn't changed and every image has reached its target, the frame does nothing.
  • React: one shared requestAnimationFrame loop for every component, in two passes: all the reads first, then all the writes, so the browser never has to recalculate styles between two images.
  • No more IntersectionObserver. In 2019 I recommended it to skip images outside the viewport. In 8.0 the vanilla version dropped it: once an idle frame costs nothing, the observer was extra work for no gain.

What about CSS scroll-driven animations?

I tried them (ViewTimeline). They were smooth on iPhone, but they ignored delay and jumped when the layout changed. For a simple parallax on a stable page, they're enough, with no JavaScript at all. The library is still useful when you want the eased catch-up, the automatic scale, or older browsers.

Still a constant challenge

The 2019 sentence still holds. This time the fix didn't come from a new browser API, but from measuring what the browser actually sends instead of assuming it.

simpleParallax.js is MIT-licensed, works as a React component (including next/image) or in vanilla JS:

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.