diff options
author | BuildTools <Noah Loomans> | 2017-01-07 22:37:16 +0100 |
---|---|---|
committer | BuildTools <Noah Loomans> | 2017-01-07 22:37:16 +0100 |
commit | 549364fb691f7f092223e186033a8138c3ead2fd (patch) | |
tree | bec1b0bbc972a08ff69464853190044780eb667d /public/javascripts/scrollSnap.js | |
parent | 4e8da42863406764a659a7337e774ad216d356c9 (diff) | |
parent | a5238353ab86923bb3911e0bf2886ebcb53dfbd2 (diff) |
Merge branch 'beta'
Diffstat (limited to 'public/javascripts/scrollSnap.js')
-rw-r--r-- | public/javascripts/scrollSnap.js | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/public/javascripts/scrollSnap.js b/public/javascripts/scrollSnap.js new file mode 100644 index 0000000..167f0c1 --- /dev/null +++ b/public/javascripts/scrollSnap.js @@ -0,0 +1,55 @@ +require('smoothscroll-polyfill').polyfill() + +const self = {} +const schedule = require('./schedule') + +self._nodes = { + search: document.querySelector('#search'), + weekSelector: document.querySelector('#week-selector') +} + +self._timeoutID = null + +self._getScrollPosition = function () { + return (document.documentElement && document.documentElement.scrollTop) || + document.body.scrollTop +} + +self._handleDoneScrolling = function () { + const scrollPosition = self._getScrollPosition() + const weekSelectorHeight = self._nodes.weekSelector.clientHeight - self._nodes.search.clientHeight + if (scrollPosition < weekSelectorHeight && scrollPosition > 0) { + window.scroll({ top: weekSelectorHeight, left: 0, behavior: 'smooth' }) + } +} + +self._handleScroll = function () { + if (self._timeoutID != null) window.clearTimeout(self._timeoutID) + self._timeoutID = window.setTimeout(self._handleDoneScrolling, 500) + + const scrollPosition = self._getScrollPosition() + const weekSelectorHeight = self._nodes.weekSelector.clientHeight - self._nodes.search.clientHeight + if (scrollPosition >= weekSelectorHeight) { + document.body.classList.add('week-selector-not-visible') + } else { + document.body.classList.remove('week-selector-not-visible') + } +} + +self._handleWindowResize = function () { + const weekSelectorHeight = self._nodes.weekSelector.clientHeight - self._nodes.search.clientHeight + const extraPixelsNeeded = weekSelectorHeight - (document.body.clientHeight - window.innerHeight) + if (extraPixelsNeeded > 0) { + document.body.style.marginBottom = extraPixelsNeeded + 'px' + } else { + document.body.style.marginBottom = null + } +} + +self.startListening = function () { + window.addEventListener('scroll', self._handleScroll) +} + +schedule.on('load', self._handleWindowResize) +window.addEventListener('resize', self._handleWindowResize) +module.exports = self |