Files
30-seconds-of-code/snippets/smooth-scroll.md
Angelos Chalaris 61200d90c4 Kebab file names
2023-04-27 21:58:35 +03:00

662 B

title, tags, cover, firstSeen, lastUpdated
title tags cover firstSeen lastUpdated
Smooth scroll element into view browser,css carrots 2018-03-02T18:22:51+02:00 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