Note that there are some explanatory texts on larger screens.

plurals
  1. PODetermine if element is inside or outside visible area of containing element
    text
    copied!<p>How do I determine if the <code>.circle</code> elements in the code below are still "onscreen"? That is are they outside the visible area of the containing <code>#wrapper</code> element?</p> <p>Here's the code:</p> <pre><code>&lt;html&gt; &lt;style type="text/css"&gt; body { background-color: #000; margin: 2px; padding: 0; } #wrapper { background-color: #ccf; height: 100%; width: 100%; overflow: hidden; position: relative; } .circle { background-color: #fff; width: 80px; height: 80px; border-radius: 40px; -moz-border-radius: 40px; position: absolute; left: 0px; top: 0px; } .stopped { background-color: #ccc; } &lt;/style&gt; </code></pre> <p><code>&lt;!-- And this script: --&gt;</code></p> <pre><code>&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function randomDirection(){ var directions = ['+=','+=','+=','-=']; var direction = directions[Math.floor(Math.random()*directions.length)] var amount = Math.floor(Math.random()*40); var unit = 'px'; return direction + amount + unit; } $(document).ready(function(){ var again = function() { $('.circle:not(".stopped")').each(function(){ $(this).animate({ top: randomDirection(), left: randomDirection() }, 300); }); setTimeout(again, 300); } for (var i=0; i&lt; 50; i++) { $('.circle:first').clone().appendTo('#wrapper'); } $('.circle').click(function(){ $(this).addClass('stopped'); }); again(); }); &lt;/script&gt; </code></pre> <p><code>&lt;!-- and this body --&gt;</code></p> <pre><code>&lt;body&gt;&lt;div id="wrapper"&gt;&lt;div class="circle"&gt;&amp;nbsp;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt; &lt;/html&gt; </code></pre> <p>Basically I want to add the <code>.stopped</code> class once the <code>.circle</code> element has left the visible area of the <code>#wrapper</code> element.</p>
 

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