Sha256: 66dff1378f01ce0964cfe5c03eeba779b6c8f490bfb0094c59c267a9fe8226b8

Contents?: true

Size: 997 Bytes

Versions: 6

Compression:

Stored size: 997 Bytes

Contents

import throttle from 'lodash.throttle';

const sections = Array.prototype.slice.apply(
  document.querySelectorAll('section')
).filter(s => s.id.length > 0).reverse()

const node = document.querySelector('#content');

let current = null,
    currentLink = null;

function onScroll() {
  const sectionOnScreen = sections.find(s => s.offsetTop > 0 && s.offsetTop <= node.scrollTop + 100);

  if (sectionOnScreen.id !== current) {
    if (currentLink) {
      currentLink.classList.remove('current');
      if (currentLink.classList.contains('expandable')) currentLink.parentElement.classList.remove('current');
    }

    const node = document.querySelector(`nav a[href="#${sectionOnScreen.id}"]`);
    if (node) {
      node.classList.add('current');
      if (node.classList.contains('expandable')) node.parentElement.classList.add('current');
    }

    current = sectionOnScreen;
    currentLink = node;
  }
}

node.addEventListener('scroll', throttle(onScroll, 400, { leading: true }), false);

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rtfdoc-0.1.5 src/js/scroll.js
rtfdoc-0.1.4 src/js/scroll.js
rtfdoc-0.1.3 src/js/scroll.js
rtfdoc-0.1.2 src/js/scroll.js
rtfdoc-0.1.1 src/js/scroll.js
rtfdoc-0.1.0 src/js/scroll.js