Last active
November 22, 2020 15:37
-
-
Save sedadkosovac/661d0fd369069cad010f164b3fcd8c20 to your computer and use it in GitHub Desktop.
Turbolinks 5 Scroll Position Persistence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Turbolinks.scroll = {}; | |
document.addEventListener("turbolinks:load", ()=> { | |
const elements = document.querySelectorAll("[data-turbolinks-scroll]"); | |
elements.forEach(function(element){ | |
element.addEventListener("click", ()=> { | |
Turbolinks.scroll['top'] = document.scrollingElement.scrollTop; | |
}); | |
element.addEventListener("submit", ()=> { | |
Turbolinks.scroll['top'] = document.scrollingElement.scrollTop; | |
}); | |
}); | |
if (Turbolinks.scroll['top']) { | |
document.scrollingElement.scrollTo(0, Turbolinks.scroll['top']); | |
} | |
Turbolinks.scroll = {}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this!