Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First off, i don't think doing <code>$('body *').live</code> is a very good idea, it seems very expensive (think about the kind of calculation the browser has to do every time you move your mouse)</p> <p>That said, here is an optimized version that ignores that aspect</p> <pre><code>var $div = $('&lt;div&gt;') .css({ 'border': '1px solid red', 'position': 'absolute', 'z-index': '65535' }) .appendTo('body'); $('body *').live('mousemove', function(e) { var topElement = null; var $bodyStar = $('body *'); for(var i=0,elem;elem=$bodyStar[i];i++) { if(elem == $div[0]) continue; var $elem = $(elem); var pos = $elem.offset(); var width = $elem.width(); var height = $elem.height(); if(e.pageX &gt; pos.left &amp;&amp; e.pageY &gt; pos.top &amp;&amp; e.pageX &lt; (pos.left + width) &amp;&amp; e.pageY &lt; (pos.top + height)) { var zIndex = document.defaultView.getComputedStyle(this, null).getPropertyValue('z-index'); if(zIndex == 'auto') zIndex = $elem.parents().length; if(topElement == null || zIndex &gt; topElement.zIndex) { topElement = { 'node': $elem, 'zIndex': zIndex }; } } } if(topElement != null) { var $elem = topElement.node; $div.offset($elem.offset()).width($elem.width()).height($elem.height()); } }); </code></pre> <p>For future reference, never use jQuerys looping mechanisms if you need performance. They are all build around function calls for every iteration, which is very slow compared to a normal loop, since the call stack initiation that happens when you do a function call is a huge overhead for most iteration operations you need to do.</p> <p>Code updated to fix errors, and allow for dynamically inserted elements.</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