How to get the mouse position in js. Multi-browser compatibility code

Source: Internet
Author: User

In fact, obtaining the mouse position is just one sentence, but if you don't know the difference between browsers, the whole process will be depressing. The difference is as follows:
1. for IE, the distance between x and y axes is obtained using event. x and event. y respectively;
Use event. pageX and event. pageY to obtain the distance between x and y axes in FF;
2. the position obtained by using this method in IE is not the position where the scroll bar has been rolled, that is, it will only be counted on the edge of the browser;
FF will calculate the locations that have been rolled in;
(This place breaks down me very much. For this little difference, I have rewritten all the programs I have used and killed them ......)
Solution 1 is nothing more than determining the browser and the method used;
Solution 2 is to add document.documentelement.scrolltopand document.doc umentElement. scrollLeft In the case of Internet Explorer;
I know little about JS. If you have better suggestions, please leave a message! Thank you.
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional. dtd "> <ptml xmlns =" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv =" Content-Type "content =" text/html; charset = gb2312 "/> <title> JS obtains the modified version of the script home for the mouse location (compatible with multiple Browser IE and firefox) </title> </pead> <body> mouse X axis: <input id = xxx type = text> Y axis of the mouse: <input id = yyy type = text> </body>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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.