Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the scroll distance from bottom to scroll

I have a div container. It has a scrollbar. I want to get the distance between the scroll and the bottom

enter image description here

Tried this without any luck.

var posTop = $('.container').scrollTop() - $('.container').offset().top;

What is the correct way to get that distance height ?

like image 755
user198989 Avatar asked Dec 14 '22 09:12

user198989


2 Answers

To calculate the height of the remaining document you can subtract the position of the bottom of the element from the maximum scroll height. Try this:

$('#container').scroll(function() {
    var remaining = $(this).prop('scrollHeight') - ($(this).scrollTop() + $(this).height());
    $('#debug').text(remaining);
}).scroll();
#container {
    height: 100px;
    width: 300px;
    overflow: scroll;
    background-color: #CCC;
    position: relative;
}
#content {
    height: 1000px;
}
#debug {
    position: fixed;
    top: 10px;
    left: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="container">
    <div id="debug"></div>
    <div id="content"></div>
</div>
like image 114
Rory McCrossan Avatar answered Dec 16 '22 21:12

Rory McCrossan


We can calculate with those values,

var scrollPosition = window.pageYOffset; // current position
var windowSize     = window.innerHeight; // window height
var bodyHeight     = document.body.offsetHeight; // body height
like image 36
Shijin TR Avatar answered Dec 16 '22 21:12

Shijin TR