Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. import { Component } from 'react';
  2.  
  3. export const scrollTo = (href) => {
  4. let anchor = href.replace("/#", "");
  5. let node = document.getElementById(href);
  6. if(node){
  7. let scrollToY = node.getBoundingClientRect().top;
  8. window.scrollTo({ top: scrollToY + window.pageYOffset, behavior: 'smooth' });
  9. }
  10. }
  11.  
  12. export default class SmoothScrollAnchor extends Component {
  13. handleClick = () => {
  14. event.preventDefault();
  15. history.pushState(null, null, this.props.href);
  16. scrollTo(this.props.href);
  17. }
  18.  
  19. render(){
  20. return(
  21. <a {...this.props} onClick={this.handleClick}>{this.props.children}</a>
  22. )
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement