How to obtain the current mouse coordinate using javascript
For javascript to obtain the current mouse coordinates, you must understand the coordinates of different browsers. The Code is as follows:
The Code is as follows:
<Html>
<Head>
<Title> javascript to get the current mouse coordinate </title>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/>
<Script type = "text/javascript">
Function mousePosition (ev ){
If (ev. pageX | ev. pageY) {// firefox, chrome, and other browsers
Return {x: ev. pageX, y: ev. pageY };
}
Return {// IE browser
X: ev. clientX + document. body. scrollLeft-document. body. clientLeft,
Y: ev. clientY + document. body. scrollTop-document. body. clientTop
};
}
Function mouseMove (ev ){
Ev = ev | window. event;
Var mousePos = mousePosition (ev );
Document. getElementById ('x'). innerHTML = mousePos. x;
Document. getElementById ('y'). innerHTML = mousePos. y;
}
Document. onmousemove = mouseMove;
</Script>
<Style type = "text/css">
H3 {color: blue ;}
P {line-height: 30px; height: 30px; font-size: 14px; width: 500px ;}
Span {color: orange; font-weight: bold ;}
</Style>
</Head>
<Body>
<H3> your mouse has been tracked <P> X axis coordinate: <span id = "x"> </span> </p>
<P> Y axis coordinate: <span id = "y"> </span> </p>
</Body>
</Html>