I want to scroll to a div
element when I click a button. But unfortunately the scroll isn't working. But my console.log()
I read this Scroll to the top of the page after render in react.js and this How to scroll to an element?
import React, { useEffect, useState, useRef } from 'react'
function TeamOld() {
const myRef = useRef(null)
return (
<div>
<button class="button is-primary is-outlined mr-4 mb-2" onClick={() => {myRef.current.scrollTo(0, 0); console.log("Test")}}>Click me</button>
...
<div className="" style={{ width: "100%" }} ref={myRef}>
</div>
</div>
)
}
export default TeamOld
您正在寻找的功能是scrollIntoView
( https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView ) scrollTo
用于滚动元素本身(假设您的 div 中有滚动条) .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.