question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

scrollTo()/scrollIntoView() doesn't work after visit

See original GitHub issue

I need to scroll to a certain element on a page right after it loads. I use a Stimulus controller which listens to the turbo:load event, then searches for the element and, if found, scrolls it into view.

This works when I call the page for the first time or refresh the browser. But whenever I reach the page through Turbo.visit(), it simply does not scroll although the controller function gets called.

(I’ve tried scrollTo(), scrollIntoView() and also location.href = '#anchorId';. The controller function finds the correct element and gets its correct position on the page.)

It seems that Turbo’s scroll position restoration functions are interfering. Is there a way to prevent those? Or is there an event that fires after?

(Also happens with v7.0.0-rc.2.)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
bepcommented, Sep 5, 2021

I think @michalsznajder is right, but I’m not sure making turbo:load the last event would solve this problem.

You could get to something similar today by doing something like this in turbo:render:

requestAnimationFrame(() => { window.scrollTo(0, 123); })

The problem with that is that Turbo has already scrolled to the top so you get this flicker effect.

Ideally it would be cool to have a way to signal Turbo to not scroll, but I’m not sure how that would look like.

3reactions
michalsznajdercommented, Aug 26, 2021

I have the same problem. I think this boils down to turbo:load being fired before scroll restore logic kicks in.

Related code: ‘Visit.loadResponse()’ calls ‘Visit.render()’. In promise for Visit.render() Visit.complete() is called that dispatches turbo:load. Later Visit.render() calls ‘performScroll()’ that modifies scroll position so your Stimulus controller is no longer working.

Maybe turbo:load could be dispatched as last event in Turbo Drive?

Also #312 is related.

Read more comments on GitHub >

github_iconTop Results From Across the Web

scrollIntoView() is not working: does not taking in account ...
This means that when I try to put some element visible to the user, by scrolling the element to a visible area, it...
Read more >
Element.scrollIntoView() - Web APIs | MDN
The Element interface's scrollIntoView() method scrolls the element's ancestor containers such that the element on which scrollIntoView() is ...
Read more >
4 Ways to Scrolling to an Element in React - BOSC Tech Labs
The most simple way is to use ref to store the reference of the element that you want to scroll to. And call...
Read more >
Window sizes and scrolling - The Modern JavaScript Tutorial
For instance, if we try to scroll the page with a script in <head> , it won't work. Regular elements can be scrolled...
Read more >
Scroll a React component into view - Robin van der Vleuten
scrollIntoView() . Which does exactly what it says it does with a few nice options to modify its behavior. Scroll to element with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found