JavaScriptマウス座標の取得方法
664 ワード
x : <input id=pX name="pX" type=text >
y : <input id=pY name="pY" type=text >
function mouseMove(ev)
{
ev= ev || window.event;
var mousePos = mouseCoords(ev);
//alert(ev.pageX);
document.getElementById("pX").value = mousePos.x;
document.getElementById("pY").value = mousePos.y;
}
function mouseCoords(ev)
{
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
document.onmousemove = mouseMove;