Skip to content
Snippets Groups Projects
handleScroll.ts 498 B
Newer Older
Liliana Sanfilippo's avatar
Liliana Sanfilippo committed
// scrolling to a specific element
export const handleScroll = (scrollId: string) => {
    const scrollElement = document.getElementById(scrollId);
    if (scrollElement) {
      const elementTop = scrollElement.getBoundingClientRect().top + window.pageYOffset;
      const offset = window.innerHeight / 2 - scrollElement.offsetHeight / 2;
      const scrollPosition = elementTop - offset;
Liliana Sanfilippo's avatar
Liliana Sanfilippo committed
      window.scrollTo({
        top: scrollPosition,
        behavior: 'smooth',
      });
    }
  };