Skip to content

Instantly share code, notes, and snippets.

@lucasn-e
Created August 2, 2023 21:36
Show Gist options
  • Save lucasn-e/c079a26f280dbacfaa0cbf833c1768ab to your computer and use it in GitHub Desktop.
Save lucasn-e/c079a26f280dbacfaa0cbf833c1768ab to your computer and use it in GitHub Desktop.
Create a custom IntersectionObserver. Requires at least a target and a callback function. Optionally you can return the observer as well if you desperately feel the need to save and reuse it. Or remove it. Or whatever else you feel like doing with it,
export const customObserver = (payload = {}) => {
if (!payload.callback || typeof payload.callback != "function") return;
if (!payload.target) return;
const usedOptions = payload.options || {
root: null,
rootMargin: payload.margin || "0px",
threshold: payload.thresholds || 0,
};
const observer = new IntersectionObserver(payload.callback, usedOptions);
observer.observe(payload.target);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment