JS get the mouse coordinates instance method _javascript skill

Source: Internet
Author: User
Tags instance method
Copy code code as follows:

var restrictx;
var restricty;
var tip;
Mouse coordinates
function mouseposition (EV) {
return {
X:ev.clientx + Document.documentelement.scrollleft-document.documentelement.clientleft,
Y:ev.clienty + document.documentelement.scrolltop-document.documentelement.clienttop
};
}
Mouse events
function MouseMove (EV) {
EV = EV | | window.event;
var mousepos = mouseposition (EV);
Restrictx = mousepos.x;
Restricty = Mousepos.y;
}
Document.onmousemove = MouseMove;
Document.onclick = MouseMove;

The above code in Google and Sohu browser to get the value will be inaccurate, you need to modify, as follows:
Copy Code code as follows:

var restrictx;
var restricty;
var tip;
Mouse coordinates
function mouseposition (EV) {
var scrollleft = Document.documentElement.scrollLeft | | Document.body.scrollLeft;
var scrolltop = Document.documentElement.scrollTop | | Document.body.scrollTop;
return {
X:ev.clientx + Scrollleft-document.documentelement.clientleft,
Y:ev.clienty + scrolltop-document.documentelement.clienttop
};
}
Mouse events
function MouseMove (EV) {
EV = EV | | window.event;
var mousepos = mouseposition (EV);
Restrictx = mousepos.x;
Restricty = Mousepos.y;
}
Document.onmousemove = MouseMove;
Document.onclick = MouseMove;

Copy Code code as follows:

var scrollleft = Document.documentElement.scrollLeft | | Document.body.scrollLeft;
var scrolltop = Document.documentElement.scrollTop | | Document.body.scrollTop;

If you can get the mouse coordinates, then go to the front, otherwise use the following method to get the mouse coordinates, "| | "The following is for browsers using the WebKit kernel

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.