我试过嘲笑这个功能,但没有完全覆盖。我不确定我怎么能嘲笑getBoundingClientRect中header和dataHeader。
handleWindowScroll() {
const header = document.querySelector('.productNavLinksContainer');
const dataHeader = document.querySelector('.productNavDataContainer');
if (header && dataHeader) {
const headerPos = header.getBoundingClientRect();
const dataHeaderPos = dataHeader.getBoundingClientRect();
const sticky = header.offsetTop;
if (dataHeaderPos.top > headerPos.height) {
this.setState({
sticky: false
});
} else if (window.pageYOffset > sticky) {
this.setState({
sticky: true
});
}
} else {
return;
}
}
Run Code Online (Sandbox Code Playgroud)