-
Notifications
You must be signed in to change notification settings - Fork 202
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
1 parent
7bbdfcd
commit d9c7834
Showing
8 changed files
with
92 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* A polyfill of the InsersectionObserver | ||
* @target web | ||
*/ | ||
export class IntersectionObserverPolyfill { | ||
private _callback: IntersectionObserverCallback; | ||
private _elements: HTMLElement[] = []; | ||
|
||
public constructor(callback: IntersectionObserverCallback) { | ||
let timer: any = null; | ||
const oldCheck = this._check.bind(this); | ||
this._check = () => { | ||
if (!timer) { | ||
timer = setTimeout(() => { | ||
oldCheck(); | ||
timer = null; | ||
}, 100); | ||
} | ||
}; | ||
|
||
this._callback = callback; | ||
|
||
window.addEventListener('resize', this._check, true); | ||
document.addEventListener('scroll', this._check, true); | ||
} | ||
|
||
public observe(target: HTMLElement) { | ||
if (this._elements.indexOf(target) >= 0) { | ||
return; | ||
} | ||
this._elements.push(target); | ||
this._check(); | ||
} | ||
|
||
public unobserve(target: HTMLElement) { | ||
this._elements = this._elements.filter(item => { | ||
return item != target; | ||
}); | ||
}; | ||
|
||
private _check() { | ||
const entries: IntersectionObserverEntry[] = []; | ||
this._elements.forEach(element => { | ||
const rect = element.getBoundingClientRect(); | ||
const isVisible = ( | ||
rect.top + rect.height >= 0 && | ||
rect.top <= window.innerHeight && | ||
rect.left + rect.width >= 0 && | ||
rect.left <= window.innerWidth | ||
); | ||
|
||
if (isVisible) { | ||
entries.push({ | ||
target: element, | ||
isIntersecting: true | ||
} as any); | ||
} | ||
}); | ||
|
||
if (entries.length) { | ||
this._callback(entries, this as any); | ||
} | ||
} | ||
} |
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