@bigfishtv/cockpit
Version:
30 lines (23 loc) • 780 B
JavaScript
import $ from 'jquery'
import _throttle from 'lodash/throttle'
// @refactor to not use jquery and maybe move this to Section component
// and add multiple listeners directly to document? is this method slower/faster?
const $window = $(window)
const $document = $(document)
const listeners = []
export let scroll = $document.scrollTop()
export function addListener(func) {
if (listeners.indexOf(func) < 0) listeners.push(func)
}
export function removeListener(func) {
const index = listeners.indexOf(func)
if (index >= 0) listeners.splice(index, 1)
}
function handleScroll() {
scroll = $document.scrollTop()
const windowHeight = $window.height()
for (let func of listeners) {
func(scroll, windowHeight)
}
}
$document.on('scroll', _throttle(handleScroll, 1000 / 20))