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:
parent
9e68a43268
commit
8565bb2adf
|
@ -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));
|
||||
});
|
Loading…
Reference in New Issue