我创建了一个React组件here,我试图让它在滚动过去之后变得固定。在这种情况下,一切都按预期工作,但在滚动浏览元素高度后,它会不断地打开和关闭类。React JS粘滞导航滚动
这里的滚动功能:
handleScroll: function(event) {
// Save the element we're wanting to scroll
var el = document.querySelector("#target");
// If we've scrolled past the height of the element, add a class
if (el.getBoundingClientRect().bottom <= 0) {
console.log(el.getBoundingClientRect().bottom + " bottom");
this.setState({
headerIsActive: true
});
// If we've scrolled back up to the top of the container, remove the class
} else if (el.getBoundingClientRect().top <= 0) {
console.log(el.getBoundingClientRect().top <= 0 + " top");
this.setState({
headerIsActive: false
});
}
},
有人能告诉我什么,我做错了什么?或者将我指向正确的方向?
感谢