Sha256: b7e8af7fb2a9711610c8e91c491d6bd821f5b6b69b2bab0881c5bb443b10c84c

Contents?: true

Size: 742 Bytes

Versions: 1

Compression:

Stored size: 742 Bytes

Contents

{
  if (!!window.databookConfig?.scrollspy) {
    window.addEventListener('load', () => {
      let links = Array.from(document.querySelectorAll('.c-toc__link'));
      let headers = links.map(x => decodeURI(x.href).split('#', 2)[1]).map(x => document.getElementById(x));
      if (links.length <= 1) return;
      window.onscroll = () => {
        let scrollPosition = document.documentElement.scrollTop;
        let offsets = headers.map(x => x.offsetTop);
        offsets = offsets.map((x, i) => x - (x - ((i == 0) ? 0 : offsets[i - 1])) * .33);
        let target = offsets.findIndex(x => x > scrollPosition);
        links.forEach((link, i) => link.classList.toggle('is-active', i == target - 1));
      };
    });
  }
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
databook-theme-0.1.1 _includes/js/scrollspy.js