Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何确定React中的元素滚动位置_Javascript_Reactjs_Ecmascript 6 - Fatal编程技术网

Javascript 如何确定React中的元素滚动位置

Javascript 如何确定React中的元素滚动位置,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,我正在尝试在聊天窗口小部件中实现无限滚动。我为聊天室主体的容器设置了ref,如下所示: const containerNode = ReactDOM.findDOMNode(this.refs.container) 我还已经设置了一个事件侦听器来处理滚动: containerNode.addEventListener('scroll', this.handleScroll); 我现在试图找出如何实现这个用例,在我可以向上滚动之前,它将进行一个ajax调用 handleScroll() {

我正在尝试在聊天窗口小部件中实现无限滚动。我为聊天室主体的
容器设置了
ref
,如下所示:

const containerNode = ReactDOM.findDOMNode(this.refs.container)
我还已经设置了一个事件侦听器来处理滚动:

containerNode.addEventListener('scroll', this.handleScroll);
我现在试图找出如何实现这个用例,在我可以向上滚动之前,它将进行一个ajax调用

handleScroll() {
    console.log("make an ajax call before it scrolls all the way up")
    if (some_conditon){
      //then load more!
    }
  }

由于您要向上滚动到容器顶部,所以只需查看容器的滚动顶部即可

const offset = 100 // 100 px before the request
if (containerNode.scrollTop < offset) {
    // request more here
}
请求前常量偏移量=100//100 px if(containerNode.scrollTop<偏移量){ //在这里要求更多 }

由于您要向上滚动到容器顶部,所以只需查看容器的滚动顶部即可

const offset = 100 // 100 px before the request
if (containerNode.scrollTop < offset) {
    // request more here
}
请求前常量偏移量=100//100 px if(containerNode.scrollTop<偏移量){ //在这里要求更多 }

注意,有一种新方法可以创建对dom元素的引用:注意,有一种新方法可以创建对dom元素的引用: