Code for js and jquery to obtain the x and y coordinates of the current mouse

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Div id = "testDiv"> put it on me </div>
<Script type = "text/javascript">
$ ('# TestDiv'). mousemove (function (e ){
Var xx = e. originalEvent. x | e. originalEvent. layerX | 0;
Var yy = e. originalEvent. y | e. originalEvent. layerY | 0;
$ (This). text (xx + '---' + yy );
});
</Script>

Javascript to get the coordinates of the current mouse position
Move the mouse to display the coordinates of the current mouse position, mainly the onmousemove function.
Copy codeThe Code is as follows:
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> javascript to get the mouse position </title>
</Head>
<Body>
<Script>
Function mouseMove (ev)
{
Ev = ev | window. event;
Var mousePos = mouseCoords (ev );
Document. getElementById ("xxx"). value = mousePos. x;
Document. getElementById ("yyy"). 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;
</Script>
Mouse X axis:
<Input id = xxx type = text>
Mouse Y axis:
<Input id = yyy type = text>
</Body>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.