MediaWiki:Common.js: Difference between revisions

From Descendants of Darkness Wiki

No edit summary
No edit summary
Line 4: Line 4:
   function saveSidebarScrollPosition() {
   function saveSidebarScrollPosition() {
     var scrollPosition = document.getElementById(sidebarId).scrollTop;
     var scrollPosition = document.getElementById(sidebarId).scrollTop;
     sessionStorage.setItem('sidebarScrollPosition', scrollPosition);
    var currentPage = window.location.href;
     sessionStorage.setItem(currentPage, scrollPosition);
   }
   }


   function restoreSidebarScrollPosition() {
   function restoreSidebarScrollPosition() {
     var scrollPosition = sessionStorage.getItem('sidebarScrollPosition');
    var currentPage = window.location.href;
     var scrollPosition = sessionStorage.getItem(currentPage);
     if (scrollPosition !== null) {
     if (scrollPosition !== null) {
       document.getElementById(sidebarId).scrollTop = parseInt(scrollPosition);
       document.getElementById(sidebarId).scrollTop = parseInt(scrollPosition);
     }
     }
  }
  // Enable manual scroll restoration
  if ('scrollRestoration' in history) {
    history.scrollRestoration = 'manual';
   }
   }


Line 24: Line 21:
   // Restore scroll position when the page is loaded
   // Restore scroll position when the page is loaded
   window.addEventListener('load', restoreSidebarScrollPosition);
   window.addEventListener('load', restoreSidebarScrollPosition);
  // Restore scroll position when the history state changes (back/forward navigation)
  window.addEventListener('popstate', restoreSidebarScrollPosition);


   // Save scroll position when clicking on sidebar links or headings
   // Save scroll position when clicking on sidebar links or headings

Revision as of 00:09, 14 April 2024

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

  function saveSidebarScrollPosition() {
    var scrollPosition = document.getElementById(sidebarId).scrollTop;
    var currentPage = window.location.href;
    sessionStorage.setItem(currentPage, scrollPosition);
  }

  function restoreSidebarScrollPosition() {
    var currentPage = window.location.href;
    var scrollPosition = sessionStorage.getItem(currentPage);
    if (scrollPosition !== null) {
      document.getElementById(sidebarId).scrollTop = parseInt(scrollPosition);
    }
  }

  // Save scroll position before unloading the page
  window.addEventListener('beforeunload', saveSidebarScrollPosition);

  // Restore scroll position when the page is loaded
  window.addEventListener('load', restoreSidebarScrollPosition);

  // Restore scroll position when the history state changes (back/forward navigation)
  window.addEventListener('popstate', restoreSidebarScrollPosition);

  // Save scroll position when clicking on sidebar links or headings
  var sidebarLinks = document.querySelectorAll('#' + sidebarId + ' a');
  sidebarLinks.forEach(function (link) {
    link.addEventListener('click', saveSidebarScrollPosition);
  });

  var sidebarHeadings = document.querySelectorAll('#' + sidebarId + ' .sidebar-heading');
  sidebarHeadings.forEach(function (heading) {
    heading.addEventListener('click', saveSidebarScrollPosition);
  });
})();