JavaScript
Article
By Sam Deering

jQuery Check if Div Scrolled to End

By Sam Deering
Help us help you! You'll get a... FREE 6-Month Subscription to SitePoint Premium Plus you'll go in the draw to WIN a new Macbook SitePoint 2017 Survey Yes, let's Do this It only takes 5 min

Simple jQuery code snippet to check if you have scrolled to the end of a div and raise an event. Useful when you have an inline element (overflow:scroll) that you want to know if someones scrolled down to the bottom. Could be used to check if someone has read terms and conditions on a registration form.

$(document).ready(function(){
    
    $('div').bind('scroll',chk_scroll);
});

function chk_scroll(e)
{
    var elem = $(e.currentTarget);
    if (elem[0].scrollHeight - elem.scrollTop() == elem.outerHeight())
    {
        console.log("bottom");
    }

}

Other ways:

--ADVERTISEMENT--

//scrollTop refers to the top of the scroll position, which will be scrollHeight - offsetHeight
if( obj.scrollTop == (obj.scrollHeight - obj.offsetHeight)) { }
Login or Create Account to Comment
Login Create Account
Recommended
Sponsors
Get the most important and interesting stories in tech. Straight to your inbox, daily.Is it good?