spaCy/website/assets/js/main.js

26 lines
880 B
JavaScript
Raw Normal View History

2016-10-03 18:19:13 +00:00
//- ----------------------------------
//- 💫 MAIN JAVASCRIPT
//- ----------------------------------
2016-03-31 14:24:48 +00:00
2016-10-31 18:04:15 +00:00
'use strict'
2016-03-31 14:24:48 +00:00
2016-10-03 18:19:13 +00:00
{
2016-10-31 18:04:15 +00:00
const nav = document.querySelector('.js-nav')
const fixedClass = 'is-fixed'
let vh, scrollY = 0, scrollUp = false
2016-03-31 14:24:48 +00:00
2016-10-31 18:04:15 +00:00
const updateVh = () => Math.max(document.documentElement.clientHeight, window.innerHeight || 0)
2016-03-31 14:24:48 +00:00
2016-10-03 18:19:13 +00:00
const updateNav = () => {
2016-10-31 18:04:15 +00:00
const vh = updateVh()
const newScrollY = (window.pageYOffset || document.scrollTop) - (document.clientTop || 0)
if (newScrollY != scrollY) scrollUp = newScrollY <= scrollY
2016-10-31 18:04:15 +00:00
scrollY = newScrollY
2016-03-31 14:24:48 +00:00
2016-10-31 18:04:15 +00:00
if(scrollUp && !(isNaN(scrollY) || scrollY <= vh)) nav.classList.add(fixedClass)
else if (!scrollUp || (isNaN(scrollY) || scrollY <= vh/2)) nav.classList.remove(fixedClass)
2016-03-31 14:24:48 +00:00
}
2016-10-31 18:04:15 +00:00
window.addEventListener('scroll', () => requestAnimationFrame(updateNav))
2016-10-03 18:19:13 +00:00
}