简体   繁体   English

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

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

I'm preparing to develop infinate scroll on our website's dashboard, however I'm stuck at the moment on how to determine the bottom of the container div in my jsfiddle mockup .我正准备在我们网站的仪表板上开发无限滚动,但是我目前在如何确定我的jsfiddle 模型中容器 div 的底部被困住了

在此处输入图片说明

Original function, which works on a blank page with no container 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 CSS

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

I tried replacing window with the div being scrolled ( #scroll_screen ) in my example, but can't get the alert to trigger.在我的示例中,我尝试用滚动的 div ( #scroll_screen ) 替换window ,但无法触发警报。

How would you have approached this problem?你会如何解决这个问题?


UPDATES更新

  • Note, I created a new jsFiddle using the same code here: http://jsfiddle.net/leonwho/L9A6Q/注意,我在这里使用相同的代码创建了一个新的 jsFiddle: http : //jsfiddle.net/leonwho/L9A6Q/

  • Also I notice that my console.logs never show unless I click inside the #scroll_screen div?我还注意到,除非我在#scroll_screen div 内单击,否则我的 console.logs 永远不会显示?

  • Deleted the Codepen, got a little further with jsFiddle, using $('#scroll_screen').scroll(function(){删除了 Codepen,使用 jsFiddle 更进一步,使用$('#scroll_screen').scroll(function(){

  • Note!注意! When I remove height: 100% from the #content div, then scroll down and back up I finally get my Alert , but this is still not correct.当我从#content div 中删除height: 100% ,然后向下滚动并向上滚动时,我终于得到了Alert ,但这仍然不正确。 The Alert should happen on scroll down警报应该在向下滚动时发生

css css

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

$('#scroll_screen').height() - $('#content').height() will give a negative value, because scroll_screen 's height is always less than content 's height, and that means scroll_screen 's scrollTop will never be equal to a negative value, so replace $('#scroll_screen').height() - $('#content').height()将给出一个负值,因为scroll_screen的高度总是小于content的高度,这意味着scroll_screenscrollTop永远不会等于负值,所以替换

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

with

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

(Greater than or equal just in-case the animation skips it.) (大于或等于以防动画跳过它。)

[EDIT] I noticed it scrolls to 200 , so if ($('#scroll_screen').scrollTop() >= 200) should work. [编辑] 我注意到它滚动到200 ,所以if ($('#scroll_screen').scrollTop() >= 200)应该可以工作。

You just have to check if (the scroll distance + the window height) >= (the offset top of the element + its height) in your scroll method.您只需要在滚动方法中检查(滚动距离+窗口高度)>=(元素的顶部偏移量+其高度)。

In this case, something like :在这种情况下,类似于:

$(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.

相关问题 当用户滚动时,我希望div在页面底部滚动 - I want a div to scroll at the bottom of the page when the user scrolls 如何检测用户何时滚动到 div 的底部 - Vue - How to detect when a user scrolls to the bottom of a div - Vue 使用 jQuery 检测用户何时滚动到 div 的底部 - Detecting when user scrolls to bottom of div with jQuery 使用 React js 检测用户何时滚动到 div 的底部 - Detecting when user scrolls to bottom of div with React js 用户滚动div时如何高亮显示底角? - how to highlight bottom li when user scroll div? 如何知道用户何时滚动过去<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>? 如何使 div 滚动到其容器的底部,然后在 React 中完成后向上滚动? - How to make a div scroll to the bottom of its container then scroll back up when it's done in React? 判断是否在div中滚动到底部 - Determine whether scroll to bottom in div JQuery - 除非用户向上滚动,否则滚动并锚定到ajax驱动的div的底部 - JQuery - Scroll and anchor to bottom of ajax-driven div unless user scrolls up jQuery-滚动并锚定到ajax驱动的div的底部,除非用户向上滚动演示代码 - JQuery - Scroll and anchor to bottom of ajax-driven div unless user scrolls up with demo code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM