[b]"What's the best way to implement js scroll to element smoothly?"[/b] or [b]"How can I use js scroll to element

14 Replies, 496 Views

Honestly, the native js scroll to element is hit or miss. I’ve found that adding a tiny delay with `setTimeout` can help with the clunkiness.

```js
setTimeout(() => {
document.getElementById('yourElement').scrollIntoView({ behavior: 'smooth' });
}, 100);
```

Weird, but it works. For easing, you’d need to manually animate the scroll position using `window.scrollY` and a lerp function. Mozilla’s docs have a solid example.

Messages In This Thread



Users browsing this thread: 1 Guest(s)