Friday, May 17, 2024
196
rated 0 times [  198] [ 2]  / answers: 1 / hits: 35639  / 13 Years ago, sun, april 3, 2011, 12:00:00

I'm using Google Chrome 10 and writing JavaScript to detect scroll end.



To detect scroll end of window, the code below worked fine:



window.addEventListener(
'scroll',
function()
{
var scrollTop = document.documentElement.scrollTop ||
document.body.scrollTop;
var offerHeight = document.body.offsetHeight;
var clientHeight = document.documentElement.clientHeight;
if (offsetHeight <= scrollTop + clientHeight)
{
// Scroll end detected
}
},
false
);


Now I want to detect scroll end of the specified element, like <section id=box style=height: 500px; overflow: auto;>

This is the code that doesn't detect correctly:



document.getElementById('box').addEventListener(
'scroll',
function()
{
var scrollTop = document.getElementById('box').scrollTop;
var offerHeight = document.getElementById('box').offsetHeight;
var clientHeight = document.getElementById('box').clientHeight;
if (offsetHeight <= scrollTop + clientHeight)
{
// This is called before scroll end!
}
},
false
);


Could someone please fix my code? Thanks.


More From » google-chrome

 Answers
106

Fixed.



document.getElementById('box').addEventListener(
'scroll',
function()
{
var scrollTop = document.getElementById('box').scrollTop;
var scrollHeight = document.getElementById('box').scrollHeight; // added
var offsetHeight = document.getElementById('box').offsetHeight;
// var clientHeight = document.getElementById('box').clientHeight;
var contentHeight = scrollHeight - offsetHeight; // added
if (contentHeight <= scrollTop) // modified
{
// Now this is called when scroll end!
}
},
false
)

[#92936] Friday, April 1, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cody

Total Points: 679
Total Questions: 111
Total Answers: 111

Location: Czech Republic
Member since Thu, Aug 11, 2022
2 Years ago
;