views:

47

answers:

2

Is there a (simple) way to return the amount of elements colliding with a single element? Perhaps with jQuery?

A: 

I think there are two ways to interpret your question:

  1. Colliding elements means number of elements that are children of an another element.

  2. Colliding means elements which by appearance on the screen overlap each other.

First interpretation seems to conclude to a faster resolution but the second might require some geometry stuffed in it.

XGreen
A: 

The HTML DOM doesn't give us a clean way of finding all elements colliding (or overlapping) with a given element.

The only real way to do it is to check x/y positioning and height/width of each element to calculate collisions geometrically. It's obviously slow if you are checking all elements on a decent sized page.

There are plugins to make this easier to manage. gameQuery has a collision function that encapsulates all the logic for you. I'm sure there are other plugins as well.

jrallison