MediaWiki:Common.js: Difference between revisions

From Descendants of Darkness Wiki

No edit summary
No edit summary
Line 12: Line 12:
     var scrollPosition = sessionStorage.getItem(currentPage);
     var scrollPosition = sessionStorage.getItem(currentPage);
     if (scrollPosition !== null) {
     if (scrollPosition !== null) {
       document.getElementById(sidebarId).scrollTop = parseInt(scrollPosition);
       var sidebar = document.getElementById(sidebarId);
      sidebar.scrollTop = parseInt(scrollPosition);
     }
     }
   }
   }


   // Create a MutationObserver to detect changes in the sidebar
   // Save scroll position when the sidebar is scrolled
   var observer = new MutationObserver(function (mutations) {
   var sidebar = document.getElementById(sidebarId);
    mutations.forEach(function (mutation) {
   sidebar.addEventListener('scroll', saveSidebarScrollPosition);
      if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
        saveSidebarScrollPosition();
      }
    });
  });
 
  // Configure the observer to watch for class attribute changes in the sidebar
  var observerConfig = {
    attributes: true,
    attributeFilter: ['class'],
    subtree: true
  };
 
  // Start observing the sidebar element
  var sidebarElement = document.getElementById(sidebarId);
   if (sidebarElement) {
    observer.observe(sidebarElement, observerConfig);
  }


   // 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:19, 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 sidebar is scrolled
  var sidebar = document.getElementById(sidebarId);
  sidebar.addEventListener('scroll', 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);
})();