鼠標事件event和坐標


鼠標事件(e=e||window.event)

 

event.clientX、event.clientY

 

鼠標相對於瀏覽器窗口可視區域的X,Y坐標(窗口坐標),可視區域不包括工具欄和滾動條。IE事件和標准事件都定義了這2個屬性

 

event.pageX、event.pageY

 

類似於event.clientX、event.clientY,但它們使用的是文檔坐標而非窗口坐標。這2個屬性不是標准屬性,但得到了廣泛支持。IE事件中沒有這2個屬性。

 

event.offsetX、event.offsetY

 

鼠標相對於事件源元素(srcElement)的X,Y坐標,只有IE事件有這2個屬性,標准事件沒有對應的屬性。

 

event.screenX、event.screenY

 

鼠標相對於用戶顯示器屏幕左上角的X,Y坐標。標准事件和IE事件都定義了這2個屬性

 

知識點1:確定鼠標按鈕(event.button

實例

<html>
<head>
<script type="text/javascript">
function whichButton(event)
{
var btnNum = event.button;
if (btnNum==2)
  {
  alert("您點擊了鼠標右鍵!")
  }
else if(btnNum==0)
  {
  alert("您點擊了鼠標左鍵!")
  }
else if(btnNum==1)
  {
  alert("您點擊了鼠標中鍵!");
  }
else
  {
  alert("您點擊了" + btnNum+ "號鍵,我不能確定它的名稱。");
  }
}
</script>
</head>

<body onmousedown="whichButton(event)">
<p>請在文檔中點擊鼠標。一個消息框會提示出您點擊了哪個鼠標按鍵。</p>
</body>

</html>

知識點2:鼠標相對屏幕的距離(x=event.screenX  /  y=event.screenY ) 

實例

<html>
<head>

<script type="text/javascript">
function coordinates(event)
{
x=event.screenX
y=event.screenY
alert("X=" + x + " Y=" + y)
}

</script>
</head>
<body onmousedown="coordinates(event)">

<p>
在文檔中點擊某個位置。消息框會提示出指針相對於屏幕的 x 和 y 坐標。
</p>

</body>
</html>

知識點3:鼠標相對於瀏覽器窗口的坐標( x=event.clientX / y=event.clientY 

<html>
<head>
<script type="text/javascript">
function show_coords(event)
{
x=event.clientX
y=event.clientY
alert("X 坐標: " + x + ", Y 坐標: " + y)
}
</script>
</head>

<body onmousedown="show_coords(event)">

<p>請在文檔中點擊。一個消息框會提示出鼠標指針的 x 和 y 坐標。</p>

</body>
</html>

 

知識點4:獲取鍵盤的按鍵的 unicode(event.keyCode)

<html>
<head>
<script type="text/javascript">
function whichButton(event)
{
alert(event.keyCode)
}

</script>
</head>

<body onkeyup="whichButton(event)">
<p><b>注釋:</b>在測試這個例子時,要確保右側的框架獲得了焦點。</p>
<p>在鍵盤上按一個鍵。消息框會提示出該按鍵的 unicode。</p>
</body>

</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM