W
whytwelve13
Is there a way to find all elements that are "under" some point on the
screen in JavaScript? That is, given (x, y) find all elements of size
(w1, h1) whose absolute position compared to the document.body starting
point is (x1, y1), with constraints x1 <= x <= x1 + w1 and y1 <= y <=
y1 + w1. Assume absolute positioning of all elements (if in need, I
will try to generalize to other positioning types, but not now). Just
to note, I am searching for solution that works in at least Firefox and
IE.
A way is surely to loop through all elements and see if they satisfy
the above conditions. However, for large number of elements (1000s),
this is damn slow if you need to do quick actions lots of times. Is
there a way to do this more efficiently?
Basically, what I need is the following, the above is just a try to
solve the below. Assume you have three divs: div1, div2 and div3. div1
is a parent of div2 and div3. div2 and div3 partially overlap. Assume
z-indexes are 1, 2 and 3 for div1, div2 and div3, respectively. In this
case, div3 is on top of div2 where they overlap. When you e.g. click on
div3, it will propagate the click upwards through the hierarchy, so
div3, div1 and document.body will get the click event. However, I would
like the planar behavior of this - when I click on div3, I would like
all the divs below (in this case, div2) to get the click event.
A way to solve it is as I suggested above - loop through all elements
and see if they satisfy the conditions. In this (click) case, it
wouldn't be too slow, as these events are not so frequent, but for
events like mousemove, looping through all elements is a very time
consuming operation. Any other ways?
I was also thinking about making some 2D-sorted array of elements, so I
can use e.g. binary search to find elements quickly. I am, however, not
sure that this would be a good idea, since I would need a list of 1000s
of elements - huge structure for JavaScript. It would be necessary to
take care of all the changes in the structure of the elements (adding,
deleting, moving, resizing), so at the end, it might not be worth it.
screen in JavaScript? That is, given (x, y) find all elements of size
(w1, h1) whose absolute position compared to the document.body starting
point is (x1, y1), with constraints x1 <= x <= x1 + w1 and y1 <= y <=
y1 + w1. Assume absolute positioning of all elements (if in need, I
will try to generalize to other positioning types, but not now). Just
to note, I am searching for solution that works in at least Firefox and
IE.
A way is surely to loop through all elements and see if they satisfy
the above conditions. However, for large number of elements (1000s),
this is damn slow if you need to do quick actions lots of times. Is
there a way to do this more efficiently?
Basically, what I need is the following, the above is just a try to
solve the below. Assume you have three divs: div1, div2 and div3. div1
is a parent of div2 and div3. div2 and div3 partially overlap. Assume
z-indexes are 1, 2 and 3 for div1, div2 and div3, respectively. In this
case, div3 is on top of div2 where they overlap. When you e.g. click on
div3, it will propagate the click upwards through the hierarchy, so
div3, div1 and document.body will get the click event. However, I would
like the planar behavior of this - when I click on div3, I would like
all the divs below (in this case, div2) to get the click event.
A way to solve it is as I suggested above - loop through all elements
and see if they satisfy the conditions. In this (click) case, it
wouldn't be too slow, as these events are not so frequent, but for
events like mousemove, looping through all elements is a very time
consuming operation. Any other ways?
I was also thinking about making some 2D-sorted array of elements, so I
can use e.g. binary search to find elements quickly. I am, however, not
sure that this would be a good idea, since I would need a list of 1000s
of elements - huge structure for JavaScript. It would be necessary to
take care of all the changes in the structure of the elements (adding,
deleting, moving, resizing), so at the end, it might not be worth it.