How can I smoothly scroll an element into view using JavaScript scroll into view? or What’s the best

18 Replies, 1229 Views

For older browsers, you might need to manually animate the scroll. Here’s a quick snippet:

```js
function smoothScroll(element) {
element.scrollIntoView({ behavior: 'smooth', block: 'start' });
if (!('scrollBehavior' in document.documentElement.style)) {
// Fallback for older browsers
const targetPos = element.getBoundingClientRect().top;
window.scrollBy({ top: targetPos, behavior: 'smooth' });
}
}
```

Not perfect, but better than nothing!

Messages In This Thread



Users browsing this thread: 1 Guest(s)