Save scroll position

👋 FYI, this note is over 6 months old. Some of the content may be out of date.
let element = document.querySelector('.element')

let top = sessionStorage.getItem('element-scroll')

if (top !== null) {
element.scrollTop = parseInt(top, 10)
}

window.addEventListener('beforeunload', () => {
sessionStorage.setItem('element-scroll', element.scrollTop)
})

← Back home