繁体   English   中英

如何确定用户何时滚动到容器 div 的底部? 对于无限卷轴

[英]How to determine when user scrolls to bottom of container div? For Infinite Scroll

我正准备在我们网站的仪表板上开发无限滚动,但是我目前在如何确定我的jsfiddle 模型中容器 div 的底部被困住了

在此处输入图片说明

原始函数,在没有容器 div 的空白页面上工作

var wireInfinScroll = function() {

    console.log('in wireInfinScroll');

    $('#scroll_screen').scroll(function(){

        console.log('scrolling...');
        console.log(' ');

        //if ($('#scroll_screen').scrollTop() == $('#content').height() -     $('#scroll_screen').outerHeight()) {
  
        if ($('#scroll_screen').scrollTop() == $('#content').height() - $('#scroll_screen').height()) {
            // run our call for pagination
            console.log('Bottom of Page!');
            alert('Bottom of Page!');
        }
    });
}

wireInfinScroll();

CSS

#scroll_screen {
  overflow-y: auto;
  background: pink;
}

在我的示例中,我尝试用滚动的 div ( #scroll_screen ) 替换window ,但无法触发警报。

你会如何解决这个问题?


更新

  • 注意,我在这里使用相同的代码创建了一个新的 jsFiddle: http : //jsfiddle.net/leonwho/L9A6Q/

  • 我还注意到,除非我在#scroll_screen div 内单击,否则我的 console.logs 永远不会显示?

  • 删除了 Codepen,使用 jsFiddle 更进一步,使用$('#scroll_screen').scroll(function(){

  • 注意! 当我从#content div 中删除height: 100% ,然后向下滚动并向上滚动时,我终于得到了Alert ,但这仍然不正确。 警报应该在向下滚动时发生

css

   #content {
     float: right;
     width: 79%;
     //height: 100%;
     background: #f8f8f8;
   }

$('#scroll_screen').height() - $('#content').height()将给出一个负值,因为scroll_screen的高度总是小于content的高度,这意味着scroll_screenscrollTop永远不会等于负值,所以替换

$('#scroll_screen').scrollTop() == $('#scroll_screen').height() - $('#content').height()

$('#scroll_screen').scrollTop() >=  if ( $('#scroll_screen').scrollTop() >= -($('#content').height() - $('#scroll_screen').height()) ))

(大于或等于以防动画跳过它。)

[编辑] 我注意到它滚动到200 ,所以if ($('#scroll_screen').scrollTop() >= 200)应该可以工作。

您只需要在滚动方法中检查(滚动距离+窗口高度)>=(元素的顶部偏移量+其高度)。

在这种情况下,类似于:

$(window).scroll(function(){
  if( ($(document).scrollTop() + $(window).height()) >= ($('#yourElement').offset().top + $('#yourElement').height()){
    // Bottom of the element reached :)
  }
});

如何知道用户何时滚动过去<div> ?</div><div id="text_translate"><p> 我正在构建一些东西,向用户展示他们没有看到的项目。</p><p> 每个项目都在一个&lt;div&gt;中,因此当用户滚动过去一个 div 或查看该 div 时,我希望将该项目标记为已被看到。</p><p> 谷歌阅读器会这样做,如果您滚动浏览提要中的某个项目,它会自动将其标记为已读。</p><p> 如何跟踪? 请指教。</p><p> 注意:不应仅限于使用鼠标滚动、向下/向上翻页、使用箭头键等也应计算在内。 主要标准是用户已经看到了一个 div。</p></div>

[英]How to know when a user scrolls past a <div>?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM