Detecting when user scrolls to bottom of div with jQuery

前端 未结 15 1847
一个人的身影
一个人的身影 2020-11-22 14:47

I have a div box (called flux) with a variable amount of content inside. This divbox has overflow set to auto.

Now, what I am trying to do, is, when the use scroll t

15条回答
  •  渐次进展
    2020-11-22 15:27

    Here's another version.

    The key code is function scroller() which takes input as the height of the div containing the scrolling section, using overflow:scroll. It approximates 5px from the top or 10px from the bottom as at the top or bottom. Otherwise it's too sensitive. It seems 10px is about the minimum. You'll see it adds 10 to the div height to get the bottom height. I assume 5px might work, I didn't test extensively. YMMV. scrollHeight returns the height of the inner scrolling area, not the displayed height of the div, which in this case is 400px.

    
              function scroller(ourheight) {
                var ourtop=document.getElementById(\'scrolling_area\').scrollTop;
                if (ourtop > (ourheight-\''.($scrolling_area_height+10).'\')) {
                    alert(\'at the bottom; ourtop:\'+ourtop+\' ourheight:\'+ourheight);
                }
                if (ourtop <= (5)) {
                    alert(\'Reached the top\');
                }
              }
    
    
    
    
    $content="your content here";
    
    
    onscroll="var ourheight=document.getElementById(\'scrolling_area\').scrollHeight; scroller(ourheight);" >'.$content.'
    '; ?>

提交回复
热议问题