MediaWiki:Common.js: Difference between revisions
From Descendants of Darkness Wiki
No edit summary Tag: Manual revert |
No edit summary |
||
| Line 6: | Line 6: | ||
var scrollPosition = document.getElementById(sidebarId).scrollTop; | var scrollPosition = document.getElementById(sidebarId).scrollTop; | ||
var currentPage = window.location.href; | var currentPage = window.location.href; | ||
sessionStorage.setItem(currentPage, scrollPosition); | sessionStorage.setItem(currentPage, scrollPosition); | ||
} catch (error) { | } catch (error) { | ||
| Line 17: | Line 16: | ||
var currentPage = window.location.href; | var currentPage = window.location.href; | ||
var scrollPosition = sessionStorage.getItem(currentPage); | var scrollPosition = sessionStorage.getItem(currentPage); | ||
if (scrollPosition !== null) { | if (scrollPosition !== null) { | ||
var sidebar = document.getElementById(sidebarId); | var sidebar = document.getElementById(sidebarId); | ||
if (sidebar) { | if (sidebar) { | ||
sidebar.scrollTop = parseInt(scrollPosition); | sidebar.scrollTop = parseInt(scrollPosition); | ||
} | } | ||
} | } | ||
| Line 35: | Line 30: | ||
document.addEventListener('visibilitychange', function () { | document.addEventListener('visibilitychange', function () { | ||
if (document.visibilityState === 'hidden') { | if (document.visibilityState === 'hidden') { | ||
saveSidebarScrollPosition(); | saveSidebarScrollPosition(); | ||
} | } | ||
| Line 43: | Line 37: | ||
document.addEventListener('visibilitychange', function () { | document.addEventListener('visibilitychange', function () { | ||
if (document.visibilityState === 'visible') { | if (document.visibilityState === 'visible') { | ||
restoreSidebarScrollPosition(); | restoreSidebarScrollPosition(); | ||
} | } | ||
| Line 51: | Line 44: | ||
var sidebar = document.getElementById(sidebarId); | var sidebar = document.getElementById(sidebarId); | ||
if (sidebar) { | if (sidebar) { | ||
sidebar.addEventListener('scroll', | sidebar.addEventListener('scroll', saveSidebarScrollPosition); | ||
} | } | ||
// | // Restore scroll position when the initial HTML document has been completely loaded and parsed | ||
document.addEventListener('DOMContentLoaded', restoreSidebarScrollPosition); | |||
})(); | })(); | ||
Revision as of 11:20, 14 April 2024
(function () {
var sidebarId = 'mw-navigation';
function saveSidebarScrollPosition() {
try {
var scrollPosition = document.getElementById(sidebarId).scrollTop;
var currentPage = window.location.href;
sessionStorage.setItem(currentPage, scrollPosition);
} catch (error) {
console.error('Error saving scroll position:', error);
}
}
function restoreSidebarScrollPosition() {
try {
var currentPage = window.location.href;
var scrollPosition = sessionStorage.getItem(currentPage);
if (scrollPosition !== null) {
var sidebar = document.getElementById(sidebarId);
if (sidebar) {
sidebar.scrollTop = parseInt(scrollPosition);
}
}
} catch (error) {
console.error('Error restoring scroll position:', error);
}
}
// Save scroll position when the page is hidden (user navigates away)
document.addEventListener('visibilitychange', function () {
if (document.visibilityState === 'hidden') {
saveSidebarScrollPosition();
}
});
// Restore scroll position when the page is visible (user comes back)
document.addEventListener('visibilitychange', function () {
if (document.visibilityState === 'visible') {
restoreSidebarScrollPosition();
}
});
// Save scroll position when the sidebar is scrolled
var sidebar = document.getElementById(sidebarId);
if (sidebar) {
sidebar.addEventListener('scroll', saveSidebarScrollPosition);
}
// Restore scroll position when the initial HTML document has been completely loaded and parsed
document.addEventListener('DOMContentLoaded', restoreSidebarScrollPosition);
})();