Answers for "how to scroll to an element javascript react"

1

react how to scroll to element

import React, { useRef } from 'react'

const scrollToRef = (ref) => window.scrollTo(0, ref.current.offsetTop)   
// General scroll to element function

const ScrollDemo = () => {

   const myRef = useRef(null)
   const executeScroll = () => scrollToRef(myRef)

   return (
      <> 
         <div ref={myRef}>I wanna be seen</div> 
         <button onClick={executeScroll}> Click to scroll </button> 
      </>
   )
}
Posted by: Guest on June-12-2020
2

how to scroll to an element javascript react

import { scroller } from "react-scroll";

// excluded React component syntax...

  scrollToSection = () => {
    scroller.scrollTo("your_css_class_here", {
      duration: 800,
      delay: 0,
      smooth: "easeInOutQuart",
    });
  };
Posted by: Guest on June-23-2020
1

react how to scroll to element

class ReadyToScroll extends Component {

    constructor(props) {
        super(props)
        this.myRef = React.createRef()  
    }

    render() {
        return <div ref={this.myRef}></div> 
    }  

    scrollToMyRef = () => window.scrollTo(0, this.myRef.current.offsetTop)   
    // run this method to execute scrolling. 

}
Posted by: Guest on June-12-2020

Code answers related to "how to scroll to an element javascript react"

Code answers related to "Javascript"

Browse Popular Code Answers by Language