diff --git a/snippets/smoothScroll.md b/snippets/smoothScroll.md new file mode 100644 index 000000000..de024bf68 --- /dev/null +++ b/snippets/smoothScroll.md @@ -0,0 +1,18 @@ +### 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 fooBar +smoothScroll('.fooBar'); // scrolls smoothly to the first element with a class of fooBar +``` diff --git a/tag_database b/tag_database index 1674e3730..a50d43b54 100644 --- a/tag_database +++ b/tag_database @@ -229,6 +229,7 @@ shuffle:array,random similarity:array,math size:object,array,string sleep:function,promise +smoothScroll:browser,css sortCharactersInString:string sortedIndex:array,math sortedIndexBy:array,math,function