summaryrefslogtreecommitdiffstats
path: root/assets/js/reading-progress.js
blob: ee1192f5e78098a6dfe7a8e3eee05ac70d45f21a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Reading progress bar for single pages/articles
(function() {
  const progressBar = document.getElementById('reading-progress');

  if (!progressBar) return;

  function updateProgress() {
    const windowHeight = window.innerHeight;
    const documentHeight = document.documentElement.scrollHeight - windowHeight;
    const scrollProgress = documentHeight > 0 ? (window.scrollY / documentHeight) * 100 : 0;
    progressBar.style.width = scrollProgress + '%';
  }

  // Throttle the scroll event for better performance
  let ticking = false;

  window.addEventListener('scroll', function() {
    if (!ticking) {
      window.requestAnimationFrame(function() {
        updateProgress();
        ticking = false;
      });
      ticking = true;
    }
  }, false);

  // Initial call
  updateProgress();
})();