Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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> 
      </>
   )
}
Comment

scroll to section react

import React, { useRef } from 'react'

const scrollToRef = (ref) => window.scrollTo({ top: ref.current.offsetTop, behavior: "smooth" })   
// General scroll to element function

const ScrollToSection = () => {

   const sectionRef = useRef(null)
   const executeSectionScroll = () => scrollToRef(myRef)

   return (
      <> 
         <div ref={sectionRef}>I wanna be seen</div> 
         <button onClick={executeSectionScroll}> Click to scroll </button> 
      </>
   )
}
Comment

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. 

}
Comment

PREVIOUS NEXT
Code Example
Javascript :: react native run on specific emulator 
Javascript :: loopback float type 
Javascript :: how to append more elements after click in react 
Javascript :: javascript remove element from array 
Javascript :: javascript select option value onchange 
Javascript :: jquery get current row value 
Javascript :: set nested state react hooks spread operator 
Javascript :: lua manifest code 
Javascript :: equation+ automate + expression reguliere 
Javascript :: how to detect js module was required 
Javascript :: ryan dahl 
Javascript :: jspdf addimage auto height 
Javascript :: jquery get selected checkboxes 
Javascript :: find array object value is already in use 
Javascript :: javascript before reload page alert 
Javascript :: javascript keep only letters in string 
Javascript :: - Root composer.json requires tymon/jwt-auth ^0.5.12 - satisfiable by tymon/jwt-auth[0.5.12]. 
Javascript :: js add delay 
Javascript :: 16/27.5 
Javascript :: how to send post request js fetch 
Javascript :: Do not know how to serialize a BigInt 
Javascript :: count occurrences of character in string javascript 
Javascript :: create array initialize size javascript 
Javascript :: react material ui input max length 
Javascript :: Sailsdock 
Javascript :: js random int 
Javascript :: how to add event listener to iframe 
Javascript :: js get meta content 
Javascript :: how to update version of dependencies reactjs 
Javascript :: background transparent react native 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =