JavaScript gets the current mouse coordinate method

Source: Internet
Author: User

For JavaScript to obtain the current mouse coordinates, you have to understand the coordinates of different browsers. The specific code is as follows:

code is as follows: <html>


<head>


<title>javascript get current mouse coordinates </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 etc browser


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 </h3>


<p> X-axis coordinates: <span id= "x" ></span></p>


<p> y axis coordinates: <span id= "y" ></span></p>


</body>


</html>

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.