您如何从引用中检索pageYOffset?当我尝试此操作时,它返回的状态不确定。
//button to get offset
<button style={{position:"fixed", top:"200px", right:"300px"}}
onClick={() =>{
console.log(refTwo.current.pageYOffset)
}}> test</button>
....
<div ref={refTwo} className={two}> hello </div>
(最好不是scrollY,因为我不想看到滚动了多少,有时页面更改会使人们在页面的中间位置。)
在构造函数中,如下所示定义引用。还要使用该参考将参考分配给div。
constructor(props) {
super(props);
this.refTwo = React.createRef();
}
<div ref={this.refTwo} />
使用]访问>
this.refTwo.current.pageYOffset