MediaWiki:Common.js: Difference between revisions

From Descendants of Darkness Wiki

No edit summary
No edit summary
Line 2: Line 2:
     var sidebarId = 'mw-navigation';
     var sidebarId = 'mw-navigation';


     // Save the scroll position before the page unloads
     // Save the sidebar scroll position before the page unloads
     window.addEventListener('beforeunload', function () {
     window.addEventListener('beforeunload', function () {
         localStorage.setItem('sidebarScrollPosition', document.getElementById(sidebarId).scrollTop);
         localStorage.setItem('sidebarScrollPosition', document.getElementById(sidebarId).scrollTop);
     });
     });


     // Restore the scroll position after the new page loads
     // Restore the sidebar scroll position after the new page loads
     window.addEventListener('DOMContentLoaded', function () {
     window.addEventListener('DOMContentLoaded', function () {
         var scrollPosition = localStorage.getItem('sidebarScrollPosition');
         var scrollPosition = localStorage.getItem('sidebarScrollPosition');
Line 15: Line 15:
     });
     });


     // Save the scroll position when clicking on sidebar links
     // Save the sidebar scroll position when clicking on sidebar links
     document.querySelectorAll('#' + sidebarId + ' a').forEach(function (link) {
     document.querySelectorAll('#' + sidebarId + ' a').forEach(function (link) {
         link.addEventListener('click', function () {
         link.addEventListener('click', function (event) {
             var scrollPosition = document.getElementById(sidebarId).scrollTop;
             var scrollPosition = document.getElementById(sidebarId).scrollTop;
             localStorage.setItem('sidebarScrollPosition', scrollPosition);
             localStorage.setItem('sidebarScrollPosition', scrollPosition);
            // Allow the default link behavior to navigate to the target page
            return true;
         });
         });
    });
    // Restore the main content scroll position after the new page loads
    window.addEventListener('DOMContentLoaded', function () {
        var hash = window.location.hash;
        if (hash) {
            var target = document.querySelector(hash);
            if (target) {
                target.scrollIntoView({
                    behavior: 'auto'
                });
            }
        }
     });
     });
})();
})();

Revision as of 19:43, 6 April 2024

(function () {
    var sidebarId = 'mw-navigation';

    // Save the sidebar scroll position before the page unloads
    window.addEventListener('beforeunload', function () {
        localStorage.setItem('sidebarScrollPosition', document.getElementById(sidebarId).scrollTop);
    });

    // Restore the sidebar scroll position after the new page loads
    window.addEventListener('DOMContentLoaded', function () {
        var scrollPosition = localStorage.getItem('sidebarScrollPosition');
        if (scrollPosition !== null) {
            document.getElementById(sidebarId).scrollTop = parseInt(scrollPosition);
        }
    });

    // Save the sidebar scroll position when clicking on sidebar links
    document.querySelectorAll('#' + sidebarId + ' a').forEach(function (link) {
        link.addEventListener('click', function (event) {
            var scrollPosition = document.getElementById(sidebarId).scrollTop;
            localStorage.setItem('sidebarScrollPosition', scrollPosition);

            // Allow the default link behavior to navigate to the target page
            return true;
        });
    });

    // Restore the main content scroll position after the new page loads
    window.addEventListener('DOMContentLoaded', function () {
        var hash = window.location.hash;
        if (hash) {
            var target = document.querySelector(hash);
            if (target) {
                target.scrollIntoView({
                    behavior: 'auto'
                });
            }
        }
    });
})();