@ebay/ebayui-core
Version:
Collection of core eBay components; considered to be the building blocks for all composite structures, pages & apps.
32 lines (31 loc) • 893 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.onScrollEnd = onScrollEnd;
/**
* Checks on an interval to see if the element is scrolling.
* When the scrolling has finished it then calls the function.
*
* @param el The element that scrolls.
* @param fn The function to call after scrolling completes.
* @return A function to cancel the scroll listener.
*/
function onScrollEnd(el, fn) {
let timeout;
let frame;
let lastPos;
(function checkMoved() {
const { scrollLeft } = el;
if (lastPos !== scrollLeft) {
lastPos = scrollLeft;
timeout = setTimeout(() => {
frame = requestAnimationFrame(checkMoved);
}, 90);
return;
}
fn(lastPos);
})();
return () => {
clearTimeout(timeout);
cancelAnimationFrame(frame);
};
}