|
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 { |
4 | 4 | if (isStr(imgList)) |
5 | | - imgList = document.querySelectorAll(imgList); |
| 5 | + imgList = document.querySelectorAll(imgList) |
6 | 6 | if (imgList.length !== undefined) |
7 | 7 | imgList = [...imgList] |
8 | 8 | const observer = new IntersectionObserver( |
9 | 9 | (entries, observer) => { |
10 | | - entries.forEach(entry => { |
| 10 | + entries.forEach((entry) => { |
11 | 11 | /* 替换属性 */ |
12 | 12 | console.log(entry.isIntersecting) |
13 | 13 | 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) |
16 | 16 | } |
17 | | - }); |
| 17 | + }) |
18 | 18 | }, |
19 | | - { rootMargin, root, threshold }); |
| 19 | + { rootMargin, root, threshold }) |
20 | 20 | 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) |
23 | 23 | } |
24 | 24 |
|
25 | | - |
26 | | - |
0 commit comments