Files
30-seconds-of-code/snippets/smoothScroll.md
2018-03-02 17:23:09 +01:00

18 lines
544 B
Markdown

### smoothScroll
Smoothly scrolls the element on which it's called into the visible area of the browser window.
Use `.scrollIntoView` method to scroll the element. Pass `{ behavior: 'smooth' }` to `.scrollIntoView` so it scrolls smoothly.
```js
const smoothScroll = element =>
document.querySelector(element).scrollIntoView({
behavior: 'smooth'
});
```
```js
smoothScroll('#fooBar'); // scrolls smoothly to the element with the id of fooBar
smoothScroll('.fooBar'); // scrolls smoothly to the element with the class of fooBar
```