ldc-legaulois/resources/js/front/functions.js
2021-03-17 09:54:41 +01:00

31 lines
1.1 KiB
JavaScript
Vendored

window.__Gaul = {
isDescendant: function (parent, child) {
var node = child.parentNode;
while (node != null) {
if (node == parent) {
return true;
}
node = node.parentNode;
}
return false;
},
debounce: (fn) => {
// This holds the requestAnimationFrame reference, so we can cancel it if we wish
let frame;
// The debounce function returns a new function that can receive a variable number of arguments
return (...params) => {
// If the frame variable has been defined, clear it now, and queue for next frame
if (frame)
cancelAnimationFrame(frame);
// Queue our function call for the next frame
frame = requestAnimationFrame(() => {
// Call our function and pass any params we received
fn(...params);
});
}
}
}
document.addEventListener('scroll', __Gaul.debounce(() => {
document.documentElement.dataset.scroll = window.scrollY;
}), { passive: true });