javascript - How to determine elements scroll position in React - Stack Overflow

I am trying to implement an infinite scroll in my chat widget. I have a ref setup for chat body's

I am trying to implement an infinite scroll in my chat widget. I have a ref setup for chat body's container like this:

const containerNode = ReactDOM.findDOMNode(this.refs.container)

I also already setup an event listener that handle's the scroll:

containerNode.addEventListener('scroll', this.handleScroll);

I am now trying to find out how to implement the use case where before i can scroll up, it would make an ajax call.

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

I am trying to implement an infinite scroll in my chat widget. I have a ref setup for chat body's container like this:

const containerNode = ReactDOM.findDOMNode(this.refs.container)

I also already setup an event listener that handle's the scroll:

containerNode.addEventListener('scroll', this.handleScroll);

I am now trying to find out how to implement the use case where before i can scroll up, it would make an ajax call.

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

Share Improve this question edited Jan 31, 2019 at 15:40 Amirhossein Mehrvarzi 19k7 gold badges50 silver badges73 bronze badges asked Apr 6, 2017 at 22:08 edmamertoedmamerto 8,15512 gold badges53 silver badges81 bronze badges 1
  • Note, that there's a new way of creating references to a dom element: reactjs/docs/refs-and-the-dom.html – fraxture Commented Dec 26, 2017 at 16:24
Add a ment  | 

1 Answer 1

Reset to default 4

Since you are scrolling up to the top all you need to do is look at the scroll top of your container.

const offset = 100 // 100 px before the request
if (containerNode.scrollTop < offset) {
    // request more here
}

SAMPLE FIDDLE

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744222958a4563872.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信