使用Reactjs获取滚动位置 [英] Get scroll position with Reactjs

查看:90
本文介绍了使用Reactjs获取滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用reactjs并希望用点击事件处理滚动。

I use reactjs and want to handle scroll with click event.

首先,我渲染了帖子列表使用 componentDidMount

Firstly, I rendered list of posts with componentDidMount.

其次,按点击事件 on列表中的每个帖子,它将显示帖子详细信息并滚动到顶部(因为我将帖子详细信息放到页面的顶部位置)。

Secondly, by click event on each post in list, It will display post detail and scroll to top (because I put post detail to top position of page).

第三,点击关闭按钮在帖子详细信息中,它会返回上一个帖子列表,但我希望网站会滚动到确切点击帖子的位置。

Thirdly, by clicking "close button" in post detail, it will return previous list of posts but I want website will scroll to exactly to position of clicked post.

我这样使用:

点击事件查看帖子详细信息:

Click event to view post detail:

inSingle = (post, e) => {
   this.setState({
        post: post,
        theposition: //How to get it here?
   });
   window.scrollTo(0, 0);
}

我想获得州的位置然后我可以通过'关闭事件'完全滚动到点击帖子的位置。

I want to get state of theposition then I can do scroll exactly to position of clicked post by 'Close event'.

谢谢你这么多

推荐答案

这应该有效:

this.setState({
    post: post,
    theposition: window.pageYOffset
});

这篇关于使用Reactjs获取滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆