Browse Source

🛠️ Implemented InviewPortType interface

👀 Added checkInViewIntersectionObserver function
🌟 Configured IntersectionObserver settings
🔥 Added callback on intersection
🚀 Ready to enhance viewport handling!
main
John Doe 1 year ago
parent
commit
5399e04780
  1. 43
      src/utils/isInViewPortIntersectionObserver.ts

43
src/utils/isInViewPortIntersectionObserver.ts

@ -0,0 +1,43 @@
export interface InviewPortType {
callback: () => void;
target: HTMLElement | null;
options: IntersectionObserverInit | undefined;
freezeOnceVisible: boolean;
}
const checkInViewIntersectionObserver = ({
target,
options = { root: null, rootMargin: `0%`, threshold: 0 },
callback,
freezeOnceVisible = false,
}: InviewPortType) => {
const _funCallback: IntersectionObserverCallback = (
entries: IntersectionObserverEntry[],
observer: IntersectionObserver
) => {
entries.map((entry: IntersectionObserverEntry) => {
if (entry.isIntersecting) {
//
callback();
// ---- IF TRUE WE WILL UNOBSERVER AND FALSE IS NO
if (freezeOnceVisible) {
observer.unobserve(entry.target);
}
}
return true;
});
};
// _checkBrowserSupport-----
if (typeof window.IntersectionObserver === "undefined") {
console.error(
"window.IntersectionObserver === undefined! => Your Browser is Notsupport"
);
return;
}
const observer = new IntersectionObserver(_funCallback, options);
target && observer.observe(target);
};
export default checkInViewIntersectionObserver;
Loading…
Cancel
Save