jQuery scroll show hidden content

后端 未结 4 1607
死守一世寂寞
死守一世寂寞 2020-12-30 17:09

How do I make it so that by default, 6 div elements are shown on the page, and when a user scrolls to the bottom of the page, six more are loaded?

If you see this ex

相关标签:
4条回答
  • 2020-12-30 17:48

    I have created a very quick example, it's not optimised by does the job:

    http://jsfiddle.net/gRzPF/2/

    0 讨论(0)
  • 2020-12-30 17:54

    After a bit of experimenting, I found the perfect answer:

    http://jsfiddle.net/jackdent/gRzPF/12/

    0 讨论(0)
  • 2020-12-30 18:03

    Lets assume you have an array of divs each initialized with the document.createElement("div") or similar. Lets assume you have a large array of them.

    var myArrayOfDivs = [];//see above
    var DivsAdded = 6;//as stated, 6 were already added - index 6 is the 7th element
    $(window).scroll(function () {
        if ($(window).scrollTop() == $(document).height() - $(window).height()) {
            for(int i = 0; i < 6; i++){
             if( myArrayOfDivs.length < DivsAdded ) break;
             $("#elementToAppendIn").append(myArrayOfDivs[DivsAdded++]);
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-30 18:15

    You should be able to use something like the following:

    jQuery( window ).scroll( function( ) {
        var loadHeight = jQuery( document ).height( ) - jQuery( window ).height( );
        if( haveDivsToLoad && jQuery( window ).scrollTop( ) >= loadHeight ) {
            // fire your load function here
        }
    } );
    

    You might need to play with loadHeight to make it work to your satisfaction.

    EDIT: I added haveDivsToLoad to the check. You should make this a global (or closure) variable and set it to true or false depending on whether there are more divs to load.

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