add smoothScroll

This commit is contained in:
Stefan Feješ
2018-03-02 17:22:51 +01:00
parent 12bba74ec7
commit 77cc7bb3cd

17
snippets/smoothScroll.md Normal file
View File

@ -0,0 +1,17 @@
### 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
```