我創建了一個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
});
}
},
有人能告訴我什麼,我做錯了什麼?或者將我指向正確的方向?
感謝