Files
30-seconds-of-code/snippets/js/s/smooth-scroll.md
2023-05-07 16:07:29 +03:00

663 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Smooth scroll element into view snippet javascript
browser
css
carrots 2020-10-22T20:24:30+03:00

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.
const smoothScroll = element =>
  document.querySelector(element).scrollIntoView({
    behavior: 'smooth'
  });
smoothScroll('#fooBar'); // scrolls smoothly to the element with the id fooBar
smoothScroll('.fooBar');
// scrolls smoothly to the first element with a class of fooBar