JS - 如何平滑滚动到目标元素视图中

79 阅读1分钟
const smoothScroll = element =>
  document.querySelector(element).scrollIntoView({
    behavior: 'smooth',
  });

smoothScroll('#fooBar'); // 平滑滚动到id为fooBar的元素
smoothScroll('.fooBar'); // 平滑滚动到class为fooBar的第一个元素