Detect ScrollView has reached the end

前端 未结 7 1127
栀梦
栀梦 2020-12-23 21:32

I have a Text with long text inside a ScrollView and I want to detect when the user has scrolled to the end of the text so I can enable a button.

7条回答
  •  别那么骄傲
    2020-12-23 21:46

    As people helped here I will add the simple code they write to make reached to top and reached to bottom event and I did a little illustration to make things simpler

    {
    if(isCloseToTop(nativeEvent)){
        //do something
    }
    if(isCloseToBottom(nativeEvent)){
       //do something
    }
    }}
    >
    ...contents
    
    
    
    
    isCloseToBottom({layoutMeasurement, contentOffset, contentSize}){
       return layoutMeasurement.height + contentOffset.y >= contentSize.height - 20;
    }
    
    
    
    ifCloseToTop({layoutMeasurement, contentOffset, contentSize}){
       return contentOffset.y == 0;
    }
    

提交回复
热议问题