我如何获得裁判以向我展示其pageYOffset?

问题描述 投票:0回答:1

您如何从引用中检索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,因为我不想看到滚动了多少,有时页面更改会使人们在页面的中间位置。)

reactjs onscroll refs
1个回答
0
投票

在构造函数中,如下所示定义引用。还要使用该参考将参考分配给div。

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

<div ref={this.refTwo} />

使用]访问>

this.refTwo.current.pageYOffset

参考:https://reactjs.org/docs/refs-and-the-dom.html

© www.soinside.com 2019 - 2024. All rights reserved.