MediaWiki:Common.js: Difference between revisions

From Descendants of Darkness Wiki

No edit summary
No edit summary
Line 16: Line 16:
     }
     }
   }
   }
  // 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
   // Save scroll position when the sidebar is scrolled
Line 23: Line 37:
   // 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);
})();
})();

Revision as of 00:21, 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) {
      var sidebar = document.getElementById(sidebarId);
      sidebar.scrollTop = parseInt(scrollPosition);
    }
  }

  // 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);
  sidebar.addEventListener('scroll', saveSidebarScrollPosition);

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