I found this: How to tell if a DOM element is visible in the current viewport?.
When I put it in my JS and force a scroll, it is not reporting the correct information.
function inViewport(e) {
//special bonus for those using jQuery
if (e instanceof jQuery) {
e = e[0];
}
var rect = e.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
);
}
$('html,body').animate({
scrollTop: $("#8").offset().top
});
var divsVisible = [];
$('div[id]').each(function() {
if (inViewport(this)) {
divsVisible.push( this.id );
}
});
debug.log(divsVisible);
My HTML file is here: https://dl.dropboxusercontent.com/u/163931/index.html
I would expect the array to have '8' in it. Instead it has '1' in it. Mac/Safari.