react-anchor-link-smooth-scroll icon indicating copy to clipboard operation
react-anchor-link-smooth-scroll copied to clipboard

pragmatic call

Open alainib opened this issue 5 years ago • 1 comments

hello

your package work very well specially the working offset. thanks for work

i'm in a case where i need to call the scroll to an element only after data is fetched. Is there a way to get a function who do that instead of AnchorLink

something like this for example ?


import { AnchorLink , scrollToAnchor} from "react-anchor-link-smooth-scroll";


fetchData( id ) {
   .....
  scrollToAnchor(id); 
}

thanks

alainib avatar Oct 22 '20 08:10 alainib

edit: i ended with this function created based on your code, work for me

import smoothscroll from 'smoothscroll-polyfill';
smoothscroll.polyfill();

export function scrollToAnchor(id, offset = 0) {
  try {
    const $anchor = document.getElementById(id);
    const offsetTop = $anchor.getBoundingClientRect().top + window.pageYOffset;
    console.log("on scroll to anchor")
    window.scroll({
      top: offsetTop - offset,
      behavior: 'smooth'
    })
  } catch (error) {
    console.error("error in scrollToAnchor", error);
  }
}

alainib avatar Oct 22 '20 09:10 alainib