-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
102 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,85 @@ | ||
let imgObserver: IntersectionObserver | null = null | ||
|
||
let imgObserverOptions: { | ||
root: HTMLElement | null | ||
} | null | ||
|
||
const imgObserverCallback: IntersectionObserverCallback = (entries) => { | ||
entries.forEach((entry) => { | ||
if (entry.isIntersecting) { | ||
const img = entry.target as HTMLImageElement | ||
if (!img.src) { | ||
img.src = img.dataset.src || '' | ||
} | ||
} | ||
}) | ||
export type IntersectionObserverOptions = Omit< | ||
IntersectionObserverInit, | ||
'root' | ||
> & { | ||
root?: Element | Document | null | string | ||
} | ||
|
||
export const imgObserverHandler: ( | ||
el: HTMLImageElement | null, | ||
root?: string | ||
) => void = (el, root = 'body') => { | ||
if (el === null) return | ||
if (imgObserver === null) { | ||
imgObserverOptions = { | ||
root: document.querySelector(root) | ||
export const resolveOptionsAndHash = ( | ||
options: IntersectionObserverOptions | undefined = {} | ||
): { | ||
hash: string | ||
options: Omit<IntersectionObserverInit, 'root'> & { root: Element | Document } | ||
} => { | ||
const { root = null } = options | ||
return { | ||
hash: `${options.rootMargin || '0px 0px 0px 0px'}-${ | ||
Array.isArray(options.threshold) | ||
? options.threshold.join(',') | ||
: options.threshold ?? '0' | ||
}`, | ||
options: { | ||
...options, | ||
root: | ||
(typeof root === 'string' ? document.querySelector(root) : root) || | ||
document.documentElement | ||
} | ||
imgObserver = new IntersectionObserver( | ||
imgObserverCallback, | ||
imgObserverOptions | ||
) | ||
} | ||
imgObserver.observe(el) | ||
} | ||
|
||
export const imgUnobserverHandler: (el: HTMLImageElement | null) => void = ( | ||
el | ||
) => { | ||
if (el === null) return | ||
if (imgObserver) { | ||
imgObserver.unobserve(el) | ||
const observers = new WeakMap< | ||
Document | Element, | ||
Map<string, [IntersectionObserver, number]> | ||
>() | ||
|
||
export const observeIntersection: ( | ||
el: HTMLImageElement | null, | ||
options: IntersectionObserverOptions | undefined | ||
) => () => void = (el, options) => { | ||
if (!el) return () => {} | ||
const resolvedOptionsAndHash = resolveOptionsAndHash(options) | ||
let rootObservers: Map<string, [IntersectionObserver, number]> | ||
const _rootObservers = observers.get(resolvedOptionsAndHash.options.root) | ||
if (_rootObservers) { | ||
rootObservers = _rootObservers | ||
} else { | ||
rootObservers = new Map() | ||
observers.set(resolvedOptionsAndHash.options.root, rootObservers) | ||
} | ||
let observer: IntersectionObserver | ||
if (rootObservers.has(resolvedOptionsAndHash.hash)) { | ||
const observerAndObservedElementCount = | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
rootObservers.get(resolvedOptionsAndHash.hash)! | ||
observer = observerAndObservedElementCount[0] | ||
observerAndObservedElementCount[1]++ | ||
} else { | ||
observer = new IntersectionObserver((entries) => { | ||
entries.forEach((entry) => { | ||
if (entry.isIntersecting) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
const img = entry.target as HTMLImageElement | ||
if (!img.src) { | ||
img.src = img.dataset.src || '' | ||
} | ||
} | ||
}) | ||
}) | ||
rootObservers.set(resolvedOptionsAndHash.hash, [observer, 1]) | ||
} | ||
observer.observe(el) | ||
return () => { | ||
const observerAndObservedElementCount = rootObservers.get( | ||
resolvedOptionsAndHash.hash | ||
) | ||
if (observerAndObservedElementCount) { | ||
observerAndObservedElementCount[0].unobserve(el) | ||
observerAndObservedElementCount[1]-- | ||
if (observerAndObservedElementCount[1] <= 0) { | ||
rootObservers.delete(resolvedOptionsAndHash.hash) | ||
} | ||
if (!rootObservers.size) { | ||
observers.delete(resolvedOptionsAndHash.options.root) | ||
} | ||
} | ||
} | ||
} |
This file contains 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
另外就是这里只要发现了一次就不需要再 observe 了,也许要处理