Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pragmatic call #34

Open
alainib opened this issue Oct 22, 2020 · 1 comment
Open

pragmatic call #34

alainib opened this issue Oct 22, 2020 · 1 comment

Comments

@alainib
Copy link

alainib commented Oct 22, 2020

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
Copy link
Author

alainib commented Oct 22, 2020

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);
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant