The following answer from that question works if you remove the jQuery cruft (which throws an error if you don't have a global variable called jQuery):
[deleted code]
Edit
Based on various answers from the link in the OP, following seems to work, only lightly tested but it works in the OP's fiddle. It checks if an element is inside its parents and the viewport. Hopefully the comments are sufficient:
// Return true if an element overlaps its parents and the viewport
function isVisible(element) {
return isInParents(element) && isInViewport(element);
}
// Return true if an element overlaps its parents
function isInParents(el) {
var rect = el.getBoundingClientRect(),
rectP,
visible = true;
while (el && el.parentNode && el.parentNode.getBoundingClientRect && visible) {
el = el.parentNode;
rectP = el.getBoundingClientRect();
visible = rectInRect(rectP, rect);
}
return visible;
}
// Return true if element overlaps the viewport
function isInViewport (element) {
var rect = element.getBoundingClientRect();
return rectInRect({top:0, left:0,
bottom: window.innerHeight || document.documentElement.clientHeight,
right: window.innerWidth || document.documentElement.clientWidth
}, rect);
}
// Return true if r1 overlaps r0
function rectInRect(r0, r1) {
return r1.top < r0.bottom &&
r1.bottom > r0.top &&
r1.left < r0.right &&
r1.right > r0.left;
}
As to whether the element is visible or not depends on other factors, such as whether overlapping elements are hidden or not, or whether some other non–ancestor element is positioned on top, etc. Those conditions can be checked for but it becomes less and less efficient the more you have to check.
If thoroughness and performance matter, create a binary tree index of the spatial location of all elements on the page and update it as you go. Creating the index is slow, but checking position will be hugely faster.