Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know how much of this is supported in older or not-so-modern browsers, but I'm using something like this (without the neeed for any libraries):</p> <pre><code>function visible(element) { if (element.offsetWidth === 0 || element.offsetHeight === 0) return false; var height = document.documentElement.clientHeight, rects = element.getClientRects(), on_top = function(r) { var x = (r.left + r.right)/2, y = (r.top + r.bottom)/2; return document.elementFromPoint(x, y) === element; }; for (var i = 0, l = rects.length; i &lt; l; i++) { var r = rects[i], in_viewport = r.top &gt; 0 ? r.top &lt;= height : (r.bottom &gt; 0 &amp;&amp; r.bottom &lt;= height); if (in_viewport &amp;&amp; on_top(r)) return true; } return false; } </code></pre> <p>It checks that the element has an area > 0 and then it checks if any part of the element is within the viewport and that it is not hidden "under" another element (actually I only check on a single point in the center of the element, so it's not 100% assured -- but you could just modify the script to itterate over all the points of the element, if you really need to...).</p> <p><em>Update</em></p> <p>Modified on_top function that check every pixel:</p> <pre><code>on_top = function(r) { for (var x = Math.floor(r.left), x_max = Math.ceil(r.right); x &lt;= x_max; x++) for (var y = Math.floor(r.top), y_max = Math.ceil(r.bottom); y &lt;= y_max; y++) { if (document.elementFromPoint(x, y) === element) return true; } return false; }; </code></pre> <p>Don't know about the performance :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload