How can I define an element at an arbitrary position of the mouse (x, y)?

Possible duplicate:
Get item at specified position - JavaScript

How can I detect elements at an arbitrary position of the mouse (x, y)?

I use mouseenter / mouseleave to highlight hovering elements. Unfortunately, none of these lights when the mouse moves implicitly, and this leads to the fact that the wrong element stands out. An example of moving a mouse implicitly would be scrolling a page using the arrow keys or track.

You can see a working demo with comments here: http://jsfiddle.net/bkG2K/6/

My idea of ​​a workaround is to check the position of the mouse as often or immediately after scrolling, if possible, and update the hover state based on the current coordinates of the mouse. But I'm not sure how I can find DOM elements given X, Y.

Ideas? If you have a better solution to the root problem, feel free to!

+3
source share
2 answers

You can use your own JavaScript method elementFromPoint(x, y), which returns the element in x, y coordinates in the viewport.

See elementFromPoint w3c project

And, an example code:

<html>
<head>
<title>elementFromPoint example</title>

<script type="text/javascript">

function changeColor(newColor)
{
 elem = document.elementFromPoint(2, 2);
 elem.style.color = newColor;
}
</script>
</head>

<body>
<p id="para1">Some text here</p>
<button onclick="changeColor('blue');">blue</button>
<button onclick="changeColor('red');">red</button>
</body>
</html>

setInterval() , , .hover(...) css , .

+1

, , , .hover(...), .

setInterval() .mousemove() ( , , )

0

All Articles