pageX
只读特性在
MouseEvent
interface returns the X (horizontal) coordinate (in pixels) at which the mouse was clicked, relative to the left edge of the entire document.
This includes any portion of the document not currently visible.
Being based on the edge of the document as it is, this property takes into account any horizontal scrolling of the page. For example, if the page is scrolled such that 200 pixels of the left side of the document are scrolled out of view, and the mouse is clicked 100 pixels inward from the left edge of the view, the value returned by
pageX
will be 300.
Originally, this property was defined as a
long
integer. The
CSSOM View Module
redefined it as a
double
float. See the
浏览器兼容性
章节了解细节。
见 Page in Coordinate systems for some additional information about coordinates specified in this fashion.
var pageX = MouseEvent.pageX;
A floating-point number of pixels from the left edge of the document at which the mouse was clicked, regardless of any scrolling or viewport positioning that may be in effect.
This property was originally specified in the Touch Events specification as a long integer, but was redefined in the CSSOM View Module to be a double-precision floating-point number to allow for subpixel precision. Even though numeric types both are represented by
Number
in JavaScript, they may be handled differently internally in the browser's code, resulting in potential behavior differences. See
浏览器兼容性
to learn which browsers have been updated to use the revised data type.
Let's take a look at a simple example that shows you the mouse's position relative to the page's origin. Since this example is presented in an
<iframe>
, that top-left corner is the top-left corner of the frame, not the browser window.
var box = document.querySelector(".box");
var pageX = document.getElementById("x");
var pageY = document.getElementById("y");
function updateDisplay(event) {
pageX.innerText = event.pageX;
pageY.innerText = event.pageY;
}
box.addEventListener("mousemove", updateDisplay, false);
box.addEventListener("mouseenter", updateDisplay, false);
box.addEventListener("mouseleave", updateDisplay, false);
The JavaScript code uses
addEventListener()
to register the function
updateDisplay()
as the event handler for the
mousemove
,
mouseenter
,和
mouseleave
事件。
updateDisplay()
simply replaces the contents of the
<span>
elements meant to contain the X and Y coordinates with the values of
pageX
and
pageY
.
<div class="box">
<p>
Move the mouse around in this box to watch its coordinates change.
</p>
<p>
<code>pageX</code>: <span id="x">n/a</span>
</p>
<p>
<code>pageY</code>: <span id="y">n/a</span>
</p>
</div>
The HTML is simple; the box we'll be watching for mouse events on is given the class
"box"
. It has two
<span>
elements, one with the ID
"x"
and one with the ID
"y"
. Those will be updated each time an event occurs to contain the latest mouse coordinates relative to the page.
The CSS used for this example is shown below.
.box {
width: 400px;
height: 250px;
border: 2px solid darkblue;
background-color: blue;
color: white;
font: 16px "Zilla", "Open Sans", "Helvetica", "Arial", sans-serif;
}
Try this out here:
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSSOM (CSS 对象模型) 视图模块
The definition of 'pageX' in that specification. |
工作草案 |
Redefined from
long
to
double
.
|
|
触摸事件
The definition of 'pageX' in that specification. |
未知 | 初始定义。 |
Prior to being added to the CSSOM View specification,
pageX
and
pageY
were available on the
UIEvent
interface in a limited subset of browsers for a short time.
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
pageX
|
Chrome 45 | Edge 12 | Firefox Yes | IE 9 | Opera Yes | Safari Yes | WebView Android 45 | Chrome Android 45 | Firefox Android Yes | Opera Android Yes | Safari iOS Yes | Samsung Internet Android 5.0 |
Value type changed from
long
to
double
|
Chrome 56 | Edge ≤79 | Firefox No | IE ? | Opera ? | Safari ? | WebView Android 56 | Chrome Android 56 | Firefox Android No | Opera Android ? | Safari iOS ? | Samsung Internet Android 6.0 |
完整支持
不支持
兼容性未知
实验。期望将来行为有所改变。
MouseEvent