Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

与scroll相关的方法 #12

Open
Witee opened this issue Dec 21, 2020 · 0 comments
Open

与scroll相关的方法 #12

Witee opened this issue Dec 21, 2020 · 0 comments
Labels

Comments

@Witee
Copy link
Owner

Witee commented Dec 21, 2020

与scroll相关的方法

window对象下:

window.scrollY (别名: window.pageYOffset) 返回文档在垂直方向已滚动的像素值, 为了跨浏览器兼容,一般用别名。

window.scrollX (别名: window.pageXOffset) 返回文档/页面水平方向滚动的像素值, 为了跨浏览器兼容,一般用别名。

window.scroll(x-coord, y-coord) window.scrollTo(x-coord, y-coord) 滚动窗口至文档中的特定位置。

Element对象下:

clientHeightoffsetHeight属性和元素的滚动、位置没有关系, 它代表元素的高度.

  • clientHeight: 包括 pending 但不包括 border、水平滚动条、margin的元素高度.对于inline的元素这个属性一直是0, 单位 px, 只读.
    image

当本元素的子元素比本元素高, 且 overflow=scroll 时, 本元素会scroll.

  • scrollHeight: 代表包括当前不可见部分元素的高度, 可见部分是 clientHeight, scrollHeight>=clientHeight, 单位 px, 只读.

    image

  • scrollTop: 代表在有滚动条时, 滚动条向下滚动的距离, 即元素顶部被遮住部分的高度. 在没有滚动条时scrollTop=0. 单位 px , 读写(设置scrollTop=0时相当于滚动到顶部).

    image

HTMLElement对象下:

  • offsetHeight: 包括paddingborder、水平滚动条, 但不包括margin的元素高度. 对于inline元素这个属性一直是0, 单位 px, 只读.

    image

  • offsetTop: 当前元素顶部距离最近父元素顶部的距离, 与滚动条没有关系. 单位 px, 只读.
    image

@Witee Witee added the Browser label Dec 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant