mirror of
https://github.com/iSharkFly-Docs/opensearch-docs-cn
synced 2025-02-12 07:44:55 +00:00
* 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>
27 lines
721 B
JavaScript
27 lines
721 B
JavaScript
let siteNav = document.querySelector('.site-nav');
|
|
const key = 'scroll';
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
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', () => {
|
|
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));
|
|
}); |