diff --git a/snippets/smoothScroll.md b/snippets/smoothScroll.md new file mode 100644 index 000000000..0e3d3aa94 --- /dev/null +++ b/snippets/smoothScroll.md @@ -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 +```