我正在使用此功能来加载产品的下一页,一旦仅剩下800px的文档要滚动,就会调用该页面。它似乎正在工作,但是我不确定这是否是最好的方法。提示?最佳做法?这是ES6类的内部内容,因此,我将仅展示相关内容,并去除所有分页逻辑。
observeAndLoad() {
const body_height = document.body.offsetHeight;
const window_height = window.innerHeight;
const scrollY = window.pageYOffset;
const trigger = scrollY >= body_height - window_height - 800;
if(document.readyState === 'complete' && trigger && !this.loading) {
this.loading = true;
window.removeEventListener('scroll', this.observeAndLoad, true);
this.loadPage();
}
}
async loadPage() {
//Fetch collection page and insert it into the tile container
try {
const elements = await new Promise((resolve, reject) => getNextPage());
document.getElementById('Products').insertAdjacentHTML('beforeend', elements);
if(this.collection.current_page !== this.ending_page) {
window.addEventListener('scroll', this.observeAndLoad, true);
this.loading = false;
}
}catch(error) {
console.log(error);
}
}