Function scrollToElement

  • Smoothly scroll to a specific element inside a container. It uses the element's bounding box to calculate the scroll position and allows for custom top and left padding as well as scroll duration.

    Parameters

    Returns Promise<void>

    Example

    // Scroll to an element with a static duration
    scrollToElement({
    container: window,
    element: document.getElementById('element_20')!,
    top: 60,
    duration: 500,
    });

    // Scroll to an element with a dynamic duration
    scrollToElement({
    element: document.getElementById('element_20')!,
    top: 60,
    duration: (px) => px,
    });

    // Use promise to handle when the scroll finishes
    scrollToElement({
    element: document.getElementById('element_20')!,
    top: 60,
    })
    .then(() => console.log('Scroll complete'))
    .catch(() => console.error('Scroll failed'));