Skip to content

CSR scroll behaviour tends to break while lenis is actively animating scroll value #452

@damnsamn

Description

@damnsamn

We're encountering an issue on our site where sometimes, when you click a link, it'll take you to part-way down the linked page, rather than snapping to the top. I've narrowed the "sometimes" down to "while lenis is animating scroll".

The core problem seems to be:

  • It seems like setting window.scrollTop or calling window.scrollTo do not function whilst lenis is currently animating scroll
  • CSR (eg Next router) will manually call some variation of window.scrollTop(0,0) when a routing change occurs

The sequence in practice looks something like this:

  1. The user scrolls some amount
  2. While the scroll value is still lerping (typically while it's eased to be single-digit or fractional pixels per frame), the user clicks a link
  3. The router updates the route, and calls its window.scrollTo or equivalent to snap the window to the top of the new page
  4. Since lenis is still hijacking the scroll value, the scroll value does not snap to the top. It instead maintains its lerp to the original target scroll position (which might be greater than the new page's total scrollHeight), but now within the context of the new page.
  5. The end result is the user is now scrolled partway down the new page.

This issue should be replicable on any project that uses some degree of client-side routing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions