Skip to content

Instantly share code, notes, and snippets.

@lucasn-e
Last active November 22, 2024 20:32
Show Gist options
  • Select an option

  • Save lucasn-e/c079a26f280dbacfaa0cbf833c1768ab to your computer and use it in GitHub Desktop.

Select an option

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.
export const customObserver = (settings = {}) => {
const { target, callback, options, margin, thresholds, immediate, caller, debug } = settings;
if (!caller && debug) console.warn("arg.caller TYPE:String unset - unable to trace call origin")
if (!target) return console.warn("arg.target TYPE:HTML:Node required" + (caller ? ' caller: ' + caller : ''));
if (typeof callback !== "function") return console.warn("arg.callback TYPE:Function required" + (caller ? ' caller: ' + caller : ''));
const usedOptions = options || {
root: null,
rootMargin: margin || "0px",
threshold: thresholds || 0,
};
const observer = new IntersectionObserver(callback, usedOptions);
if (immediate) observer.observe(target);
return observer;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment