Last active
November 22, 2024 20:32
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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