Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Data From Server While Scrolling a Div to Bottom

I need to load data inside a div only when I reached near the bottom of the div through scroll.

The problem am facing is the $('div').scrollHeight() and the $('div').scrollTop() values are not same when it reaches the bottom, so I can't identify that the scroll reached the bottom or not.

How can I identify the div scroll have reached near the bottom(or may be bottom minus some length)?

Please advice...

Note: Basically am trying to do the same like in Facebook ticker.

Regards, Navin

like image 560
Navin Leon Avatar asked Jul 09 '26 01:07

Navin Leon


2 Answers

There's lots of scroller samples on the web the code below is from this one.

...
    if(container.scrollTop+container.clientHeight == container.scrollHeight){
                startRow = parseInt(startRow) + 10;
                getData();
    }
...

Hope that helps.

like image 151
Nishant Sharma Avatar answered Jul 11 '26 14:07

Nishant Sharma


Heres a JSFiddle scroll it uses scrollTop + clientHeight to compare against scrollHeight. See if that works for you, sorry its in Mootools but it pretty straight forward.

function HandleScroll()
{
    var el = $("outer");
    if( (el.scrollTop + el.clientHeight) >= el.scrollHeight )
    {
        el.setStyles( { "background-color": "red"} );
    }
    else
    {
        el.setStyles( { "background-color": "white"} );
    }
}
like image 45
Dampsquid Avatar answered Jul 11 '26 14:07

Dampsquid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!