From 3173e689e2fabfc0a69d13c91744b7e8b8e89155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Fri, 2 Mar 2018 17:22:51 +0100 Subject: [PATCH] add smoothScroll --- snippets/smoothScroll.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 snippets/smoothScroll.md 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 +```