scroll.js 776 B

12345678910111213141516171819202122
  1. export function storeScrollInformationInHtmlBeforeNavigatingAway() {
  2. document.body.setAttribute('data-scroll-x', document.body.scrollLeft)
  3. document.body.setAttribute('data-scroll-y', document.body.scrollTop)
  4. document.querySelectorAll('[x-navigate\\:scroll]').forEach(el => {
  5. el.setAttribute('data-scroll-x', el.scrollLeft)
  6. el.setAttribute('data-scroll-y', el.scrollTop)
  7. })
  8. }
  9. export function restoreScrollPosition() {
  10. let scroll = el => {
  11. el.scrollTo(Number(el.getAttribute('data-scroll-x')), Number(el.getAttribute('data-scroll-y')))
  12. el.removeAttribute('data-scroll-x')
  13. el.removeAttribute('data-scroll-y')
  14. }
  15. scroll(document.body)
  16. document.querySelectorAll('[x-navigate\\:scroll]').forEach(scroll)
  17. }