Scroll to top when last visit was more than 24 hours ago (#2457)

* Scroll to top when last visit was more than 24 hours ago

Signed-off-by: Fanit Kolchina <kolchfa@amazon.com>

* Changed localStorage to sessionStorage

Signed-off-by: Fanit Kolchina <kolchfa@amazon.com>

Signed-off-by: Fanit Kolchina <kolchfa@amazon.com>
This commit is contained in:
kolchfa-aws 2023-01-26 11:14:26 -05:00 committed by GitHub
parent 9e68a43268
commit 8565bb2adf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 5 deletions

View File

@ -1,12 +1,27 @@
let siteNav = document.querySelector('.site-nav');
const key = 'scroll';
document.addEventListener('DOMContentLoaded', () => {
const scroll = localStorage.getItem('scroll');
if (scroll !== null) {
siteNav.scrollTop = parseInt(scroll);
const scroll = JSON.parse(sessionStorage.getItem(key));
const currentDate = new Date();
if (scroll !== null && currentDate.getTime() < scroll.expiry) {
siteNav.scrollTop = parseInt(scroll.value);
}
else {
sessionStorage.removeItem(key);
}
});
window.addEventListener('beforeunload', () => {
localStorage.setItem('scroll', siteNav.scrollTop);
});
const currentDate = new Date();
// add the scroll value that expires after one day
const scroll = {
value: siteNav.scrollTop,
expiry: currentDate.getTime() + 24 * 60 * 60 * 1000,
}
sessionStorage.setItem(key, JSON.stringify(scroll));
});