Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore scroll position after reload #1

Closed
kirillplatonov opened this issue Sep 3, 2021 · 6 comments · Fixed by #40
Closed

Restore scroll position after reload #1

kirillplatonov opened this issue Sep 3, 2021 · 6 comments · Fixed by #40
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@kirillplatonov
Copy link
Owner

No description provided.

@kirillplatonov kirillplatonov added help wanted Extra attention is needed enhancement New feature or request labels Jan 14, 2022
@tomazzlender
Copy link

tomazzlender commented Jul 28, 2022

One way to solve the restore of the vertical scroll position after a reload.

function scrollToStoredPosition() {
  let scrollPosition = localStorage.getItem("scrollPosition")
  window.scrollTo(0, parseInt(scrollPosition))
}

function storePosition() {
  localStorage.setItem("scrollPosition", window.scrollY)
}

document.addEventListener("turbo:before-visit", function (event) {
  storePosition()
})

document.addEventListener("turbo:load", function (event) {
  scrollToStoredPosition()
})

document.addEventListener("DOMContentLoaded", function (event) {
  scrollToStoredPosition()
})

document.addEventListener("turbo:frame-load", function (event) {
  scrollToStoredPosition()
})


window.onscroll = function (event) {
  if (window.scrollY === 0) {
    // It covers a mysterious edge case.
    // Sometimes a page jumps to the top, fires the onscroll event and sets it to 0, when it shouldn't.
    // This seems to do the trick.
    scrollToStoredPosition()
  } {
    storePosition()
  }
}

Put the code into app/javascript/application.js or somewhere similar.

@kirillplatonov
Copy link
Owner Author

Thank you! I'll try it.

@joanriba
Copy link

I pasted the code above in my applicaton.js but still not working. Scroll is moving up after each realod. What amb I missing?

@n1xn
Copy link

n1xn commented Dec 22, 2022

Would be really nice if this feature would come out of the box as it is kind of expected.

@davidalejandroaguilar
Copy link

davidalejandroaguilar commented Jan 13, 2023

@tomazzlender This worked, thanks.

I just removed the mysterious edge case hack because it would cause the screen to jump a bit when manually scrolling to the top of the page.

@webdesignweisshart
Copy link

@tomazzlender
Nice script. Only one question:
The three addEventListener("turbo:... presumably require a locally installed script. Right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants