Ben
Ben

Reputation: 2564

Detect div scroll bar hit the top

how to detect when div scroll bar, scroll up and hit the top(only when it hit the top)

I have found another post, but this one is hit the bottom.

what i need is hit the top. anyone know how to change this?

$("#divID").scroll(function(){
    if($(this)[0].scrollHeight - $(this).scrollTop() <= $(this).outerHeight())
    {
        alert(1);
    }
});

Know when vertical scrollbar hits bottom of scroll in div

Upvotes: 3

Views: 2879

Answers (3)

zkanoca
zkanoca

Reputation: 9918

$("#divID").scroll(function(){
    if($(this)[0].scrollHeight - $(this).scrollTop() <= $(this).outerHeight())
    {
        alert("Bottom");
    }
    if($(this).scrollTop() == 0)
    {
        alert("Top");
    }
});

Fiddle is here

Upvotes: 0

HIRA THAKUR
HIRA THAKUR

Reputation: 17757

working fiddle

var el = $('.test');
el.on('scroll', function(){

    if(el.scrollTop() == 0){alert("i just hit the top")}


});

Upvotes: 4

Rory McCrossan
Rory McCrossan

Reputation: 337560

scrollTop will be 0 when the scroll bar is that the top. Try this:

$("#divID").scroll(function () {
    if ($(this).scrollTop() == 0) {
        alert(1);
    }
});

Example fiddle

Upvotes: 1

Related Questions