Created
November 28, 2017 02:19
-
-
Save mohnish/6b183c3550f58a06d1c179bbb374733c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Setup. | |
*/ | |
const items = document.querySelectorAll('.Page') | |
const links = document.querySelectorAll('.Menu a') | |
/** | |
* Check if `el` is out out of view. | |
*/ | |
function isBelowScroll(el) { | |
return el.getBoundingClientRect().bottom > 0 | |
} | |
/** | |
* Activate item `i`. | |
*/ | |
function activateItem(i) { | |
links.forEach(e => e.classList.remove('active')) | |
links[i].classList.add('active') | |
} | |
/** | |
* Activate the correct menu item for the | |
* contents in the viewport. | |
*/ | |
function activate() { | |
let i = 0 | |
for (; i < items.length; i++) { | |
if (isBelowScroll(items[i])) { | |
break | |
} | |
} | |
activateItem(i) | |
} | |
/** | |
* Activate scroll spy thingy. | |
*/ | |
window.addEventListener('scroll', e => activate()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment