仿照微信分页上滑加载更多

有些话、适合烂在心里 提交于 2020-08-08 11:20:31
<div className="right_Sco" id="scoID" onScrollCapture={() => this.handleOnScroll()} ref={dom => {this.dom = dom}}>
    
</div>

  .right_Sco{
    overflow-y: scroll;
    height: 482px;
    width:100%;
  }

    // 加载更多
     handleOnScroll=()=>{
         let that = this;
         that.handleMore();

     };
 // 处理更多
     handleMore(){
         let that = this;
         const {nextReqMessageID,conversationID,messageList,xAll} = this.state;
         if (that.dom) {
             const contentScrollTop = that.dom.scrollTop;
             var x = 0;
             if (contentScrollTop<10){
                 if (xAll === undefined){
                     x =  that.dom.scrollHeight - that.dom.scrollTop;
                 }else {
                     x = xAll;
                 }
                 console.log('当前位置===',x);
                 let promise = tim.getMessageList({conversationID: conversationID, nextReqMessageID, count: 15});
                 promise.then(function(imResponse) {
                     const messageLists = imResponse.data.messageList; // 消息列表。
                     console.log('续加=',messageLists);
                     const nextReqMessageID = imResponse.data.nextReqMessageID; // 用于续拉,分页续拉时需传入该字段。
                     const isCompleted = imResponse.data.isCompleted; // 表示是否已经拉完所有消息。
                     that.setState({
                         messageList:messageLists.concat(messageList),
                         moreFlag:isCompleted,
                         nextReqMessageID:nextReqMessageID,
                         xAll:x
                     },function () {
                         if (!isCompleted){
                             let ele = document.getElementById('scoID');
                             ele.scrollTop = x;
                             console.log('x====',x)
                         }

                     });
                 });
             }
         }
     };

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!