In a react app, I have a method being called to bring a particular node into view as follows.
scrollToQuestionNode(id) { const element = document.getElementById(id); element.scrollIntoView(false); }
The scroll happens fine, but the scroll action is a little jerky. How can I make it smooth? I don't see any options which I can give to scrollIntoView for the same.
If you press the mouse scroll wheel, you can move your mouse up/down and the scroll will be very smooth. Enabling a smooth scroll allows you to scroll like that with your regular wheel scroll. Smooth scrolling is also useful with keyboard shortcuts.
scrollIntoView on Android Browser is fully supported on 97-103, partially supported on 2.3-4, and not supported on below 2.3 Android Browser versions. scrollIntoView on Opera Mobile is fully supported on 64-64, partially supported on 10-12, and not supported on below 10 Opera Mobile versions.
The scrollIntoView() method scrolls an element into the visible area of the browser window.
The scrollTo() method of the window Interface can be used to scroll to a specified location on the page. It accepts 2 parameters the x and y coordinate of the page to scroll to. Passing both the parameters as 0 will scroll the page to the topmost and leftmost point.
This might help.
From MDN documentation of scrollIntoView You can pass in option instead of boolean.
scrollIntoViewOptions Optional A Boolean or an object with the following options: { behavior: "auto" | "instant" | "smooth", block: "start" | "center" | "end" | "nearest", inline: "start" | "center" | "end" | "nearest", }
So you can simply pass parameter like this.
scrollToQuestionNode(id) { const element = document.getElementById(id); element.scrollIntoView({ block: 'end', behavior: 'smooth' }); }
Reference: https://developer.mozilla.org/en/docs/Web/API/Element/scrollIntoView
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With