I try to scroll to an element when I scroll or use the wheel. Whatever my purpose is to do this juste one time, not on the whole website as the like shows.
The problem is, first it scrolls directly on page load, second it does not scroll on scroll or when I use the wheel.
Here is shot I did, my code :
componentDidMount() {
window.addEventListener('wheel', () => {
document.querySelector("#services").scrollIntoView(true)
})
window.addEventListener('scroll', () => {
document.querySelector("#services").scrollIntoView(true)
})
}
If I listen my logic what I do is: When wheel detected (or scroll) scroll to this point.
So my question is, why it does not work as expected ?
EDIT
I discovered that it is more glitchy with the following css
html {
scroll-behavior: smooth;
}
I tried something else that works but only without the CSS above, with the css it does not work, that obscure.
componentDidMount() {
window.addEventListener('wheel', () => {
location.hash = "#services"
})
}
So it scroll directly to the element, IF the css is not set. But it has no sense, and I need this css...