Files
30-seconds-of-code/snippets/smoothScroll.md
Isabelle Viktoria Maciohsek 5cb69e3c5c Update snippet descriptions
2020-10-22 20:24:30 +03:00

23 lines
565 B
Markdown

---
title: smoothScroll
tags: browser,css,intermediate
---
Smoothly scrolls the element on which it's called into the visible area of the browser window.
- Use `Element.scrollIntoView()` to scroll the element.
- Use `{ behavior: 'smooth' }` to scroll 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
```