Detecting when user scrolls to bottom of div with jQuery

前端 未结 15 1709
一个人的身影
一个人的身影 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:30

    not sure if it is any help but this is how I do it.

    I have an index panel that is larger that the window and I let it scroll until the end this index is reached. Then I fix it in position. The process is reversed once you scroll toward the top of the page.

    Regards.

    <style type="text/css">
        .fixed_Bot {    
                position: fixed;     
                bottom: 24px;    
            }     
    </style>
    
    <script type="text/javascript">
        $(document).ready(function () {
            var sidebarheight = $('#index').height();
            var windowheight = $(window).height();
    
    
            $(window).scroll(function () {
                var scrollTop = $(window).scrollTop();
    
                if (scrollTop >= sidebarheight - windowheight){
                    $('#index').addClass('fixed_Bot');
                }
                else {
                    $('#index').removeClass('fixed_Bot');
                }                   
            });
        });
    
    </script>
    
    0 讨论(0)
  • 2020-11-22 15:32

    If you need to use this on a div that has overflow-y as hidden or scroll, something like this may be what you need.

    if ($('#element').prop('scrollHeight') - $('#element').scrollTop() <= Math.ceil($('#element').height())) {
        at_bottom = true;
    }
    

    I found ceil was needed because prop scrollHeight seems to round, or perhaps some other reason causing this to be off by less than 1.

    0 讨论(0)
  • 2020-11-22 15:36

    I found a solution that when you scroll your window and end of a div shown from bottom gives you an alert.

    $(window).bind('scroll', function() {
        if($(window).scrollTop() >= $('.posts').offset().top + $('.posts').outerHeight() - window.innerHeight) {
            alert('end reached');
        }
    });
    

    In this example if you scroll down when div (.posts) finish its give you an alert.

    0 讨论(0)
提交回复
热议问题