What is the difference between screenX/Y, clientX/Y and pageX/Y?

后端 未结 7 1422
夕颜
夕颜 2020-11-22 13:38

What is the difference between screenX/Y, clientX/Y and pageX/Y?

Also for iPad Safari,

7条回答
  •  温柔的废话
    2020-11-22 14:26

    1. pageX/Y gives the coordinates relative to the element in CSS pixels.
    2. clientX/Y gives the coordinates relative to the viewport in CSS pixels.
    3. screenX/Y gives the coordinates relative to the screen in device pixels.

    Regarding your last question if calculations are similar on desktop and mobile browsers... For a better understanding - on mobile browsers - we need to differentiate two new concept: the layout viewport and visual viewport. The visual viewport is the part of the page that's currently shown onscreen. The layout viewport is synonym for a full page rendered on a desktop browser (with all the elements that are not visible on the current viewport).

    On mobile browsers the pageX and pageY are still relative to the page in CSS pixels so you can obtain the mouse coordinates relative to the document page. On the other hand clientX and clientY define the mouse coordinates in relation to the visual viewport.

    There is another stackoverflow thread here regarding the differences between the visual viewport and layout viewport : Difference between visual viewport and layout viewport?

    Another good resource: http://www.quirksmode.org/mobile/viewports2.html

提交回复
热议问题