Skip to content

Commit 4502b2e

Browse files
author
Simon he
committed
chore: add lazyLoad
1 parent 0d2f295 commit 4502b2e

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/lazyLoad.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
import { isArray } from "./isArray";
2-
import { isStr } from "./isStr";
3-
export function lazyLoad(imgList: any, root: Element, rootMargin: string = '0px 0px 200px 0px', threshold: any): void {
1+
import { isArray } from './isArray'
2+
import { isStr } from './isStr'
3+
export function lazyLoad(imgList: any, root: Element, rootMargin = '0px 0px 200px 0px', threshold: any): void {
44
if (isStr(imgList))
5-
imgList = document.querySelectorAll(imgList);
5+
imgList = document.querySelectorAll(imgList)
66
if (imgList.length !== undefined)
77
imgList = [...imgList]
88
const observer = new IntersectionObserver(
99
(entries, observer) => {
10-
entries.forEach(entry => {
10+
entries.forEach((entry) => {
1111
/* 替换属性 */
1212
console.log(entry.isIntersecting)
1313
if (entry.isIntersecting) {
14-
(entry.target as HTMLImageElement).src = (entry.target as any).dataset.src;
15-
observer.unobserve(entry.target);
14+
(entry.target as HTMLImageElement).src = (entry.target as any).dataset.src
15+
observer.unobserve(entry.target)
1616
}
17-
});
17+
})
1818
},
19-
{ rootMargin, root, threshold });
19+
{ rootMargin, root, threshold })
2020
if (isArray(imgList))
21-
(imgList as Element[]).forEach(img => observer.observe(img));
22-
else observer.observe(imgList as Element);
21+
(imgList as Element[]).forEach(img => observer.observe(img))
22+
else observer.observe(imgList as Element)
2323
}
2424

25-
26-

0 commit comments

Comments
 (0)