今天来讲一下offsetX,clientX,pageX,screenX,layerX,x的区别,以及在chrome,firefox,edge浏览器中的相同和不同点
下面先上一段代码:
1 | <!DOCTYPE html> |
分析结果
Chrome
offsetX — 点击位置相对padding左上角的距离,因此如果存在border,点击在border上为负值,点击在padding内为正值
clientX — 相对于浏览器左侧可视区域的距离
pageX — 相对于浏览器左侧页面的滚动距离 = clientX + 滚动条滚动距离, 因此在没有滚动条的情况下等于clientX,有滚动条的时候也可能大于screenX
screenX — 相对于显示屏左侧的距离
layerX — 点击位置相对于border左上角的距离,因此如果存在border,其值等于offsetX + border-left
x — 等于clientX
Firefox
offsetX — 与chrome一致
clientX — 与chrome一致
pageX — 与chrome一致
screenX — 与chrome一致
layerX — 与chrome一致
x — 与chrome一致
Edge
offsetX — 与chrome一致
clientX — 与chrome一致
pageX — 与chrome一致
screenX — 与chrome一致
layerX — 这个值与chrome和firefox行为不一致,其值相对于父容器的border左上角的距离
x — 与chrome一致
注意
没有scrollX,scrollY属性