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

18 Replies, 1222 Views

Pro tip: Check if your browser even supports smooth scrolling first!

```js
if ('scrollBehavior' in document.documentElement.style) {
element.scrollIntoView({ behavior: 'smooth' });
} else {
// Fallback to jQuery or something
}
```

Also, Safari can be weird with javascript scroll into view. Sometimes adding a tiny timeout fixes it:

```js
setTimeout(() => element.scrollIntoView({ behavior: 'smooth' }), 50);
```

Messages In This Thread



Users browsing this thread: 1 Guest(s)