Posted By

Leech on 07/21/06


Tagged

object test hit


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

hides
jkochis
shachi


Hit Test v1.0


 / Published in: JavaScript
 

URL: http://jsfromhell.com/geral/hittest

Checks if an object or more partially superposes the area of another. Created: 2005.11.24

  1. /**************************************
  2. * Jonas Raoni Soares Silva
  3. * http://www.joninhas.ath.cx
  4. **************************************/
  5.  
  6. hitTest = function(o, l){
  7. function getOffset(o){
  8. for(var r = {l: o.offsetLeft, t: o.offsetTop, r: o.offsetWidth, b: o.offsetHeight};
  9. o = o.offsetParent; r.l += o.offsetLeft, r.t += o.offsetTop);
  10. return r.r += r.l, r.b += r.t, r;
  11. }
  12. for(var b, s, r = [], a = getOffset(o), j = isNaN(l.length), i = (j ? l = [l] : l).length; i;
  13. b = getOffset(l[--i]), (a.l == b.l || (a.l > b.l ? a.l <= b.r : b.l <= a.r))
  14. && (a.t == b.t || (a.t > b.t ? a.t <= b.b : b.t <= a.b)) && (r[r.length] = l[i]));
  15. return j ? !!r.length : r;
  16. };

Report this snippet  

You need to login to post a comment.