2016-03-18

javascript: is Element really really visible?

/**
 * Is the element visible by the Eye?
 * @param {Element | jQuery} el - object to check. Either an Element or a jQuery selection.
 * @returns {object} True if it can be seen.
 */
function isElementVisible(el) {
    "use strict";
    if (!el) // Can't find? Can't see!
        return false;
    var v_element = (el instanceof jQuery) ? el : $(el);
    if (v_element.length === 0) // Still nothing to look for
        return false;
    return v_element[0].isSameNode(document.elementFromPoint((v_element.width() / 2) + v_element.offset().left, (v_element.height() / 2) + v_element.offset().top));
}

What happens here?

We are checking if the element at the given point (our element's middle) is the same element as returned by document.elementFromPoint: then we can see it. If a different one, then that foreign element overlays the one we are looking for.

No comments :

Post a Comment