我正在试着听卷轴活动. 以下是我转换的原始代码.
document.addEventListener("scroll", function(){
const headerChange = document.querySelector("header");
if (window.scrollY > 0) {
headerChange.classList.add("scrolled");
}
else {
headerChange.classList.remove("scrolled");
}
})
虽然这是我在我的react 代码中转换它的方式.
const [isScrolling, setIsScrolling] = useState(false);
const navRef = useRef();
const navScroll = () => {
setIsScrolling(window.scrollY > 0 ?
navRef.current.classList.add("scrolled")
: navRef.current.classList.remove("scrolled"));
console.log(navScroll)
}
return (
<nav
ref={navRef}
onScroll={() => isScrolling}>
</nav>
)
滚动页面时未能触发Scroll事件
请问,滚动功能未能触发的原因可能是什么?