我刚刚花了一些时间用 React 和滚动事件/位置来解决一些问题 - 所以对于那些仍在寻找的人,这就是我发现的:
可以使用window.innerHeight 或document.documentElement.clientHeight 找到视口高度。 (当前视口高度)
整个文档(正文)的高度可以使用window.document.body.offsetHeight找到。
如果您正在尝试查找文档的高度并知道何时到达底部 - 这就是我想出的:
if (window.pageYOffset >= this.myRefII.current.clientHeight &&
Math.round((document.documentElement.scrollTop + window.innerHeight))
< document.documentElement.scrollHeight - 72) {
this.setState({ trueOrNot: true });
} else {
this.setState({ trueOrNot: false });
}
}
(我的导航栏是 72px 在固定位置,因此 -72 以获得更好的滚动事件触发)
最后,这里有一些到 console.log() 的滚动命令,它们帮助我积极地弄清楚我的数学。
console.log('window inner height: ', window.innerHeight);
console.log('document Element client hieght: ', document.documentElement.clientHeight);
console.log('document Element scroll hieght: ', document.documentElement.scrollHeight);
console.log('document Element offset height: ', document.documentElement.offsetHeight);
console.log('document element scrolltop: ', document.documentElement.scrollTop);
console.log('window page Y Offset: ', window.pageYOffset);
console.log('window document body offsetheight: ', window.document.body.offsetHeight);
哇!希望它可以帮助某人!