I am making mobile web using React.js.
How to toggle class to div when scroll down/up only in specific div ?
This is I tried so far. My code problem is that it toggle class only when reach to top.
const [scroll, setScroll] = useState(false);
useEffect(() => {
window.addEventListener("scroll", () => {
setScroll(window.scrollY > 50);
});
}, []);
<div class="area">This area can Scroll</div>
<div className={scroll ? "hide" : "show"}>toggle class here</div>
window.scrollYwill give you the scroll value of the browser window, not the specific div. Use a reference to the element andscrollTop: stackoverflow.com/questions/4373667/…