JavaScript event get Absolute mouse position

Source: Internet
Author: User
  code is as follows copy code

Take X axis position
function MouseX (evt) {
Firefox
if (Evt.pagex) return evt.pagex;
Ie
else if (EVT.CLIENTX)
return Evt.clientx + (Document.documentElement.scrollLeft document.documentElement.scrollLeft: Document.body.scrollLeft);
else return null;
}
Take the y-axis position
function Mousey (evt) {
Firefox
if (evt.pagey) return evt.pagey;
Ie
else if (evt.clienty)
return Evt.clienty + (Document.documentElement.scrollTop document.documentElement.scrollTop: DOCUMENT.BODY.SCROLLTOP);
else return null;
}

Cases

  code is as follows copy code

function CPos (x, y)
{
this.x = x;
This.y = y;
}
Get the position of the control
function Getobjpos (atarget)
{
var target = Atarget;
var pos = new CPos (Target.offsetleft, target.offsettop);

var target = target.offsetparent;
while (target)
{
Pos.x + = Target.offsetleft;
Pos.y + = Target.offsettop;

target = Target.offsetparent
}

return POS;

}

Test

The code is as follows Copy Code

var pos = Getobjpos ($id ("T1"));
$id ("Div2"). Style.left = pos.x + "px";//"10px";
$id ("Div2"). Style.top = Pos.y * 1px;
$id ("Div2"). Style.top = Pos.y + "px";

Instance

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"
<title> New D Ocument </title>
<meta name= "generator" content= "EditPlus"
<meta name= "Author" content= "" >
<meta name= "Keywords" content= ""
<meta name= "Description" "
content=;

<body onmousemove= "Getxy (event)" >
<script language= "JavaScript" >
<!--
function MouseX (evt) {
if (Evt.pagex) return evt.pagex;
else if (EVT.CLIENTX)
return Evt.clientx + (document.documentElement.scrollLeft?
Document.documentElement.scrollLeft:
Document.body.scrollLeft);
else return null;
}
function Mousey (evt) {
if (evt.pagey) return evt.pagey;
else if (evt.clienty)
return Evt.clienty + (Document.documentElement.scrollTop?
Document.documentElement.scrollTop:
DOCUMENT.BODY.SCROLLTOP);
else return null;
}

function Getxy (event)
{
var e = Event | | window.event;
var x = MouseX (e);
var y = Mousey (e);
document.getElementById ("XY"). InnerHTML = "x:" + x + "<br/>y:" + Y;
}


function GetX (ElementID)
{
var el = ElementID
Return El.offsetleft

}

function GetY (ElementID)
{
var el = ElementID
Return El.offsettop

}

function Getelementpositionx (elemid)
{
var offsettrail = document.getElementById (elemid);
var offsetleft = 0;

while (Offsettrail)
{
Offsetleft + = Offsettrail.offsetleft;
Offsettrail = offsettrail.offsetparent;
}

if (Navigator.userAgent.indexOf ("Mac")!=-1 &&
typeof (Document.body.leftMargin)!= "undefined") {
Offsetleft + = Document.body.leftMargin;
}

return offsetleft;
}

function Getelementpositiony (elemid)
{
var offsettrail = document.getElementById (elemid);
var offsettop = 0;

while (Offsettrail)
{
offsettop + = Offsettrail.offsettop;
Offsettrail = offsettrail.offsetparent;
}

if (Navigator.userAgent.indexOf ("Mac")!=-1 &&
typeof (Document.body.leftMargin)!= "undefined") {
offsettop + = Document.body.topMargin;
}
return offsettop;
}
function Getelementpositionxy (elemid)
{
var offsettrail = Elemid;
var offsetleft = 24;
var offsettop = 0;

while (Offsettrail)
{
Offsetleft + = Offsettrail.offsetleft;
offsettop + = Offsettrail.offsettop;
Offsettrail = offsettrail.offsetparent;
}

    if (navigator.userAgent.indexOf ("Mac")!=-1 &&
         typeof (Document.body.leftMargin)!= "undefined") {
        Offsetleft + = Document.body.leftMargin;
        offsetleft + = Document.body.leftMargin;
   }
   document.getElementById ("Txt_left"). innertext = Offsetleft;
   document.getElementById ("Txt_top"). innertext = offsettop;
    document.getElementById ("Divmsg"). Style.top = offsettop + "px";
}

-->
</script>
Absolute position of mouse: <br/>
<span id= "XY" style= "color:red;" ></span><br/><br/>
The absolute position of the control input box Txtpos: <br/>
X:<span id= "Txt_left" style= "color:red;" ></span><br/>
Y:<span id= "Txt_top" style= "color:red;" ></span><br/>
<input type= "text" name= "Txtpos" id= "Txtpos" onfocus= "Getelementpositionxy (This)"/>
<br/><br/>
<div id= "divmsg" style= "Height:60px;width:100px;background-color: #cccccc" ></div>
</body>

An analysis of the event location attribute:

1. IE Event.x,event.y is the reference point (excluding the scrolling distance) from the outside of the parent element of the event triggering element.
2. Firefox's Event.pagex,event.pagey is based on the BODY element as the reference point (including scrolling distance)
3. Event.clientx,event.clienty to the top left corner of the browser (excluding scrolling distance)
4. IE's event.offsetx,event.offsety and Firefox's event.layerx,event.layery are the reference points in the upper-left corner of the event-triggering element (including scrolling distances, which may appear negative when there is a border).

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.