I know how to find out the position of the mouse relative to the top-left corner of the browser window, but I don't know how to do so relative to the top-left corner of the div box.
A:
If you get the top-left corner of the div box, you can just subtract this from the screen coordinates of the mouse.
Christopher Stott
2009-11-17 07:04:42
Of course, I would do so myself, but the problem is how to get the coordinates without relying on the browser window.
Stranger
2009-11-17 07:10:01
+1
A:
Try the follow Javascript function.
var IE = document.all?true:false
if (!IE) {
document.captureEvents(Event.MOUSEMOVE);
}
function getMousePosition(e) {
if (IE) {
var X = event.clientX + document.body.scrollLeft
var Y = event.clientY + document.body.scrollTop
} else {
var X = e.pageX
var Y = e.pageY
}
if (X < 0) {
X = 0
}
if (Y < 0) {
Y = 0
}
alert("X : "+ X +" Y: "+ Y);
}
document.onmousemove = getMousePosition;