How to capture scroll event?

后端 未结 3 764
遥遥无期
遥遥无期 2021-01-17 21:26

I want to implement infinite scrolling. Below is a short form of my layout. Since I have some elements relative positioned the javascript scroll event does not fire.

<
相关标签:
3条回答
  • 2021-01-17 22:15

    The correct way to implement it is:

     <div id="container" onScroll="handleOnScroll();">
    
    <script>
    function handleOnScroll() {
            alert("scroll");
        };
    </script>
    
    0 讨论(0)
  • 2021-01-17 22:17

    EDIT: Since you tagged your question with jquery...


    To capture the scroll event using jQuery...

    HTML:

    <div id="container">
        CONTENT
    </div> 
    

    jQuery:

    $(document).ready(function() {
    
        $('#container').scroll(function() {
            alert('scroll');
            // presumably your infinite scrolling code here
        });
    
    });
    

    See: http://api.jquery.com/scroll/

    0 讨论(0)
  • 2021-01-17 22:20

    This is what i used in my code...

     <div id="DataDiv" style="overflow: auto; width: 280px; height:400px; margin-top: 10px;"
                        onscroll="Onscrollfnction();">
          my content here 
     </div>
    

    Function is as below

    function Onscrollfnction() {
                var div = document.getElementById('DataDiv');
                div.scrollLeft;
                return false;
            };
    

    After content crossing 400px, scrolling will start and will be infinite.. enjoy

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