Re

Reference impl of inertia scrolling in vanilla JavaScript

Hacker News

Reference impl of inertia scrolling in vanilla JavaScript

Hello. In order to implement a high-performance, double-ended virtual list in a cross platform way for a personal project, I had to code my own scroll routines. Scroll anchoring is not available on Safari[1], so prepending elements to a DOM node causes scroll shifting. I tried working around it for days, but solutions were super fragile and inconsistent. Even in browsers that do have scroll anchoring, the behavior is not perfeclty consistent and performance still suffers from repaints caused by on-the-fly scroll compensation done by the browser. So I decided to use css animations, specifically transform animations, coupled with absolutely positioned elements to have full control of the list behavior. This is not something out of the ordinary to do. But the real challenge was approximating the native scroll feeling on iOS. I found very little online content regarding how to go about implementing inertia. The general solution was to apply a friction constant after having the deltas yielded by the calculated velocity/acceleration. But iOS also has this mechanism were acceleration increases as you scroll repeatedly. Also, deceleration seemed to be dynamic according to the samplings I made. The final code [2] is quite trivial, but arriving at this state took quite too many tests, since it's a very very delicate mechanism. Hence, I'm sharing the routines here to help future web-searchers save time in case they happened to fall in this rabbit hole too. There's some avenues for adjusments, the current constants/dynamics found on the code were the ones which simply appealed the most to me. Interestingly, I even came to dislike how iOS accelerates too much on repeated flicks and ended up prefering my own dynamics, go figure. [1] https://caniuse.com/css-overflow-anchor [2] https://gist.github.com/jonsecchis/18c4d5de214467f5f94cffb0b...

Share card

Actual performance

1points
Did not reach leaderboard

Launch Intel predictions

Analyze your own launch →
Indie HackersFits the IH revenue-focused audience · Strong signals: ios · Missing: supports, reddit linkedin, podcasting
95%95% predicted probability of success on Indie Hackers, based on ML models trained on real launch data.
best fitHighest predicted score across all platforms for this description.
Hacker NewsStrong engagement from HN community · Strong signals: ide, io · Missing: https docs, excited, just released
64%64% predicted probability of success on Hacker News, based on ML models trained on real launch data.
nativeThis product was originally launched on this platform.
TrustMRRFits verified-revenue profile · Strong signals: ios, personal, way · Missing: mobile apps, entrepreneurs, apps
53%53% predicted probability of success on TrustMRR, based on ML models trained on real launch data.
Product HuntUnlikely to reach the leaderboard · Strong signals: code · Missing: mac, agents, macos
44%44% predicted probability of success on Product Hunt, based on ML models trained on real launch data.
AppSumoMay struggle as an AppSumo deal · Strong signals: platform · Missing: plus, intuitive, reviews
37%37% predicted probability of success on AppSumo, based on ML models trained on real launch data.
Acquire.comPre-revenue stage for this audience · Strong signals: arr · Missing: mrr, revenue, profit
13%13% predicted probability of success on Acquire.com, based on ML models trained on real launch data.
BetaListMay not resonate with beta-testers · Missing: web3, chat, crypto
0%0% predicted probability of success on BetaList, based on ML models trained on real launch data.

Incorrect prediction on native model

Similar products

In
Interactive JavaScript API Reference57%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Interactive JavaScript API Reference

Hacker News2
Go
Go-Mnemonic – Reference Implementation of a BIP-39 Go36%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Go-Mnemonic – Reference Implementation of a BIP-39 Go

Hacker News3
Pr
Promises in JavaScript41%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Promises in JavaScript

Hacker News1
Ja
Javascript Keylogger54%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Javascript Keylogger

Hacker News1
Iv
Ivy - Bound JavaScript54%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Ivy - Bound JavaScript

Hacker News1
a
a refreshing JavaScript datepicker54%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

a refreshing JavaScript datepicker

Hacker News2
Ja
Javascript constructor overloading54%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Javascript constructor overloading

Hacker News2
Se
Sets in JavaScript36%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Sets in JavaScript

Hacker News2
A
A Drip of JavaScript52%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

A Drip of JavaScript

Hacker News31
I
I converted libmp3lame to JavaScript59%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

I converted libmp3lame to JavaScript

Hacker News3