javascript獲得滑鼠的絕對位置

來源:互聯網
上載者:User

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>

<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>
滑鼠的絕對位置:<br />
<span id="XY" style="color:red;"></span><br /><br />
控制項輸入框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>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.