作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用IntersectionObserver polyfill在图 block 上延迟加载图像。问题是我有几千个图 block ,每个图 block 上都有一个需要延迟加载的图像。
以前,我使用滚动去抖动器仅在滚动停止时加载图像,这极大地提高了性能。
问题是如何将滚动去抖动器与 IntersectionObserver 一起使用?
一个解决方案但很愚蠢的是创建一个可见项目的初步数组并添加超时
let timeoutLastEntities;
new IntersectionObserver((entries) => {
setTimeout(function(){
timeoutLastEntities.add(entities);
}, 3000);
// debouncer logic
}, { threshold: 0.5 }).observe(imageTileElements);
最佳答案
好吧,我找到了一个食谱,但它仍然不完美
private initializeLazyLoader() {
this.observer = new IntersectionObserver(
this.processLazyChanges,
{ threshold: [0.5] }
);
// When scroll is triggered
this.registerIntersectionObserverEvent(this.nativeElement, 'scroll', 300);
}
processLazyChanges(changes: any) {
changes.forEach((change: any) => {
var container = change.target;
$(container).css('border', '1px solid red');
this.observer.unobserve(container);
});
}
private registerIntersectionObserverEvent(element: any, event: any, debouncerTime: number) {
Observable.fromEvent(element, event)
.debounceTime(debouncerTime)
.subscribe((event) => this.initializeObservers(event));
}
private initializeObservers(event: any) {
Array.from(document.querySelectorAll('app-tile')).forEach((tile: any) => {
this.observer.observe(tile);
});
}
关于javascript - IntersectionObserver 去抖器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43181245/
我是一名优秀的程序员,十分优秀!