All files / src/utils elements.ts

100% Statements 9/9
100% Branches 2/2
100% Functions 2/2
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26      4x 3x                     4x 5x 5x 4x 4x            
/**
 * 画面の最上部にスムーススクロールします。
 */
export const scrollToTop = () => {
  window.scrollTo({
    top: 0,
    behavior: 'smooth',
  });
};
 
/**
 * 指定された要素へスムーススクロールします。
 * @param elementId スクロール先の要素の ID
 * @param offset スクロール位置に加算するオフセット値 (デフォルト: 0)
 */
export const scrollToElement = (elementId: string, offset = 0) => {
  const element = document.getElementById(elementId);
  if (element) {
    const elementPosition = element.getBoundingClientRect().top + window.scrollY;
    window.scrollTo({
      top: elementPosition + offset,
      behavior: 'smooth',
    });
  }
};