Search code examples
javascripthtmlruby-on-railsscrollturbolinks

Something is scrolling back to 0,0 after document loaded


I have this JS to scroll to a specific place on pageload (which, for Rails with Turbolinks, is on Turbolinks load):

document.addEventListener("turbolinks:load", function() {
  window.scrollTo(240,1);
});

But when I load a page, it loads at 240,1 for a split second, and then jerks back to 0,0.

Does anyone have an idea what is causing this delayed scroll to 0,0?

I have also tried replacing "turbolinks:load" with "page:load" and "ready", they don't work either.

Things that might be relevant:

  • Chrome desktop
  • Ruby on Rails 5.2.3
  • Turbolinks 5
  • No other custom JS

Solution

  • Try history.scrollRestoration = 'manual';. It should prevent the page from scrolling back to a previous position.