The instance method of obtaining mouse coordinates by JS

Source: Internet
Author: User
Tags instance method
This article introduces JS to obtain the mouse coordinates instance method, the need friend may refer to the 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 event


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 event


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, go to the front, otherwise get the mouse coordinates in the following way, "| | "Later, the browser for the WebKit kernel uses the

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.