In order to calculate whether an element is visible, you can create such a function (credit is due here https://stackoverflow.com/a/22480938/825240):
function isScrolledIntoView(element) {
var elementTop = element.getBoundingRect().top;
var elementBottom = element.getBoundingRect().bottom;
var isVisible = (elementTop <= window.innerHeight) && (elementBottom >= 0);
return isVisible;
}
You can customize that function to your situation by calculating if an element has been read:
function isRead(element) {
var elementTop = element.getBoundingRect().top;
var elementBottom = element.getBoundingRect().bottom;
var elementHeight = elementBottom - elementTop;
// if 75% of the document has been scrolled, we'll assume it's been read
var readIfPercentage = 0.75;
// an element has been read if the top has been scrolled up out of view
// and at least 75% of the element is no longer visible
var isRead = (elementTop < 0 && Math.abs(elementTop) / elementHeight >= readIfPercentage);
return isRead;
}
You can then call the functions above, passing in a DOM node as the element:
isScrolledIntoView(document.getElementById('targetDiv');
//or
isRead(document.getElementById('targetDiv');
You can tie it all together by creating a scroll listener (jQuery makes this pretty easy):
function setScrollListener() {
var scrollEventHandler = function() {
if (isRead(document.getElementById('article'))) {
// set article to 'read'
}
}
// on scroll, fire the event handler
$(document).scroll(scrollEventHandler);
}
It's worth noting that if you want to unbind the scroll listener, say if all of the articles have been read and you no longer need to listen to the scroll, you can call the unbind function within the scrollEventHandler. It is as simple as:
function unbindScrollEventHandler() {
$(document).unbind('scroll', scrollEventHandler);
}