Element.scrollTop - Web APIs 编辑
The Element.scrollTop
property gets or sets the number of pixels that an element's content is scrolled vertically.
An element's scrollTop
value is a measurement of the distance from the element's top to its topmost visible content. When an element's content does not generate a vertical scrollbar, then its scrollTop
value is 0
.
When scrollTop
is used on the root element (the <html>
element), the scrollY
of the window is returned. This is a special case of scrollTop
.
On systems using display scaling, scrollTop
may give you a decimal value.
Syntax
// Get the number of pixels scrolled.
var intElemScrollTop = someElement.scrollTop;
After running this code, intElemScrollTop
is an integer corresponding to the number of pixels that the element
's content has been scrolled upwards.
// Set the number of pixels scrolled.
element.scrollTop = intValue;
scrollTop
can be set to any integer value, with certain caveats:
- If the element can't be scrolled (e.g. it has no overflow or if the element has a property of "non-scrollable"),
scrollTop
is0
. scrollTop
doesn't respond to negative values; instead, it sets itself back to0
.- If set to a value greater than the maximum available for the element,
scrollTop
settles itself to the maximum value.
Example
padding-top
If you can see this, scrollTop = 0Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
If you can see this, scrollTop is > 0Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
If you can see this, scrollTop is maxed-outpadding-bottom
Left Top Right Bottom margin-top margin-bottom border-top border-bottomSpecifications
Specification | Status | Comment |
---|---|---|
CSS Object Model (CSSOM) View Module The definition of 'scrollTop' in that specification. | Working Draft |
Browser compatibility
BCD tables only load in the browser
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论