I have a div container. It has a scrollbar. I want to get the distance between the scroll and the bottom
Tried this without any luck.
var posTop = $('.container').scrollTop() - $('.container').offset().top;
What is the correct way to get that distance height ?
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With