TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: Reference impl of inertia scrolling in vanilla JavaScript

1 点作者 pilgrim08 个月前
Hello.<p>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.<p>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.<p>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&#x2F;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.<p>The final code [2] is quite trivial, but arriving at this state took quite too many tests, since it&#x27;s a very very delicate mechanism. Hence, I&#x27;m sharing the routines here to help future web-searchers save time in case they happened to fall in this rabbit hole too. There&#x27;s some avenues for adjusments, the current constants&#x2F;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.<p>[1] <a href="https:&#x2F;&#x2F;caniuse.com&#x2F;css-overflow-anchor" rel="nofollow">https:&#x2F;&#x2F;caniuse.com&#x2F;css-overflow-anchor</a><p>[2] <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;jonsecchis&#x2F;18c4d5de214467f5f94cffb0b1534c36" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;jonsecchis&#x2F;18c4d5de214467f5f94cffb0b...</a>

暂无评论

暂无评论