Sha256: 429b5a901bd7e89a035e3db5007dfc35398770b5a0cf64e53e4327501ab4aaa3
Contents?: true
Size: 1.74 KB
Versions: 3
Compression:
Stored size: 1.74 KB
Contents
; (function () { // Tell the browser not to handle scrolling when restoring via the history or // when reloading if ('scrollRestoration' in history) { history.scrollRestoration = 'manual' } var SCROLL_POSITION = 'scroll-position' var PAGE_INVALIDATED = 'page-invalidated' // Persist the scroll position on refresh addEventListener('beforeunload', function () { sessionStorage.setItem(SCROLL_POSITION, JSON.stringify(scrollData())) }); // Invalidate the page when the next page is different from the current page // Persist scroll information across pages document.addEventListener('turbo:before-visit', function (event) { if (event.detail.url !== location.href) { sessionStorage.setItem(PAGE_INVALIDATED, 'true') } sessionStorage.setItem(SCROLL_POSITION, JSON.stringify(scrollData())) }) // When a page is fully loaded: // 1. Get the persisted scroll position // 2. If the locations match and the load did not originate from a page // invalidation, // 3. scroll to the persisted position if there, or to the top otherwise // 4. Remove the persisted information addEventListener('turbo:load', function (event) { var scrollPosition = JSON.parse(sessionStorage.getItem(SCROLL_POSITION)) if (shouldScroll(scrollPosition)) { scrollTo(scrollPosition.scrollX, scrollPosition.scrollY) } else { scrollTo(0, 0) } sessionStorage.removeItem(PAGE_INVALIDATED) }); function shouldScroll(scrollPosition) { return (scrollPosition && scrollPosition.location === location.href && !JSON.parse(sessionStorage.getItem(PAGE_INVALIDATED))) } function scrollData() { return { scrollX: scrollX, scrollY: scrollY, location: location.href } } })()
Version data entries
3 entries across 3 versions & 1 rubygems