用window.event對象的屬性,總共有四對屬性用來取得鼠標位置,分別是:
clientX和clientY screenX和screenY offsetX和offsetY x和y
一 clientX和clientY
設置或接收相對于瀏覽器窗口客戶區的鼠標x、y坐標,客戶區不包括窗口滾動條及邊框
大小(MSDN原文:Sets or retrieves the x-coordinate/y-coordinate of the mouse
pointer's position relative to the client area of the window, excluding
window decorations and scroll bars.)
在IE4里,這一對屬性只讀不可寫,IE5及以后版本里則可讀可寫(MSDN原文:The
property is read-only in Microsoft® Internet Explorer 4.0, and read/write
in Microsoft® Internet Explorer 5 and later.)
語法: event.clientX=[iPos] event.clientY=[iPos] iPos應該是一個整數
二 screenX和screenY
設置或接收相對于用戶屏幕的鼠標坐標
在IE4里,這一對屬性只讀不可寫,IE5及以后版本里則可讀可寫
語法: event.screenX=[iSize] event.screenY=[iSize] iSize應該是一個整數
三 offsetX和offsetY
設置或接收鼠標指針在鼠標所在的元素上的偏移量
在IE4里,這一對屬性只讀不可寫,IE5及以后版本里則可讀可寫
語法: event.screenX=[iCoord] event.screenY=[iCoord] iCoord應該是一個整數
四 x和y
設置或接收相對于鼠標所在的元素的父元素的坐標(MSDN原文:Sets or retrieves
the x-coordinate, in pixels, of the mouse pointer's position relative to
the parent element.)
在IE4里,這一對屬性只讀不可寫,IE5及以后版本里則可讀可寫,但IE5以前的版式本里
x和y坐標并不相對于鼠標所在的元素的父元素,只相對于瀏覽器窗口的客戶區
如果在檢測鼠標位置時,鼠標卻在瀏覽器窗口的外面,則x和y都返回-1