@bigfishtv/cockpit
Version:
42 lines (33 loc) • 1.1 kB
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?
var $window = $(window);
var $document = $(document);
var listeners = [];
export var scroll = $document.scrollTop();
export function addListener(func) {
if (listeners.indexOf(func) < 0) listeners.push(func);
}
export function removeListener(func) {
var index = listeners.indexOf(func);
if (index >= 0) listeners.splice(index, 1);
}
function handleScroll() {
scroll = $document.scrollTop();
var windowHeight = $window.height();
for (var _iterator = listeners, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var func = _ref;
func(scroll, windowHeight);
}
}
$document.on('scroll', _throttle(handleScroll, 1000 / 20));