Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect when textarea is scrolled to bottom

Tags:

jquery

css

Is there a way to gauge when a user has scrolled to the bottom of an element? I know that jQuery can access the scrollTop() of an element, but even combining that with the element's height, I cannot calculate when an element has been scrolled to the bottom-most position. I would like to do this so when scrolling within a textarea I can prevent the body of the document from scrolling once the "scroll bottom" of the textarea is reached.

JSBIN

like image 525
1252748 Avatar asked Oct 28 '13 02:10

1252748


1 Answers

Here's what you're looking for: http://jsfiddle.net/BW8LT/1/

$('textarea#mytextarea').on('scroll', function() {
    var offset = 200; // height of textarea

    if (this.scrollHeight <= (this.scrollTop+offset)) {
        console.log('end of textarea!');
    }
});

Edit 1: disable scrolling by hiding the overflow

$('textarea#mytextarea').on('scroll', function() {
    var offset = 200; // height of textarea

    if (this.scrollHeight <= (this.scrollTop+offset)) {
        // hide the overflow (=> disable scrolling) when the end is reached
        $('body').css('overflow', 'hidden');
    }
});

// re-enable scrolling on mouseout
$('textarea#mytextarea').on('mouseout', function() {
    $('body').css('overflow', 'scroll');
});

Just compare textarea.scrollHeight with textarea.scrollTop.

Take a look at your console while scrolling. Hope that helps!

like image 187
Mr. B. Avatar answered Sep 22 '22 21:09

Mr. B.