What I am trying to do is detect when I've reached to the bottom.
From c_g.scrollTop
I get some value 59
when I've reached to the bottom. While the getHeight()
is 800px
.
How do I interpret this value, and how do I know I've reached for example 500px
?
Is my approach cross-browser compatible too?
<script type="text/javascript">
function getWidth() {
return Math.max(
document.body.scrollWidth,
document.documentElement.scrollWidth,
document.body.offsetWidth,
document.documentElement.offsetWidth,
document.documentElement.clientWidth
);
}
function getHeight() {
return Math.max(
document.body.scrollHeight,
document.documentElement.scrollHeight,
document.body.offsetHeight,
document.documentElement.offsetHeight,
document.documentElement.clientHeight
);
}
var w_threshold = 800,
w, h, s_t, c_cmbo, c_diff,
c_h = document.querySelector('div#c_h')
c_c = document.querySelector('div#c_c'),
c_s = document.querySelector('div#c_s'),
c_g = document.querySelector('div#c_g');
document.querySelector('div#c_g').addEventListener('scroll', eventScroll);
document.querySelector('div#c_g').addEventListener('wheel', eventScroll);
document.querySelector('div#c_g').addEventListener('touchmove', eventScroll);
refresh();
function refresh() {
w = getWidth();
h = getHeight();
c_diff = Math.round(((h-c_m.offsetHeight)/((h+c_m.offsetHeight)/2)*100));
console.log('difference = '+c_diff);
eventScroll();
}
function eventScroll() {
if(w<=w_threshold){
if(s_t != c_g.scrollTop) {
s_t = c_g.scrollTop;
console.log('- scrollTop = '+s_t);
}
} else {
c_m.style.marginBottom = 0+'px';
}
}
</script>
HTML structure:
<body>
<div id="c_g">
<div id="c_m">
<div class="shadow"></div>
<div id="c_h">
</div>
<div id="c_c">
<ul class="promo">
</ul>
</div>
<div id="c_s">
</div>
</div>
</div>