In the demo below, the height value of the outer element is 200px, and the height value of the inner element is 300px. Obviously, the "content in the outer element" is higher than the "outer element" itself. When the scroll bar is dragged downward, part of the content will be hidden outside the "upper boundary of the outer element", and scrollTop is equal to The height of this "invisible content".
Demo: (Drag the scroll bar, you can see the change of scrollTop value)
These texts are displayed in the inner element.
scrollTop value is:
Explanation:
Inside The height value of the layer element is 300px > The height value of the outer element is 200px, so the "content of the outer element" (that is, the "inner element") cannot be fully displayed, and the outer element has overflow set to auto, so the outer element An up-down sliding bar will appear on the right side of the element
In the initial state, the "upper boundary of the inner element" and the "upper boundary of the outer element" coincide with each other, and no content exceeds the "upper border of the outer element" , at this time the value of the scrollTop attribute is 0.
When the scroll bar is dragged downward, the content that exceeds the "upper boundary of the outer element" will gradually increase, and the scrollTop value is equal to these excess parts.
When the scroll bar is dragged to the bottom, the "lower border of the inner element" and the "lower border of the outer element" coincide, and the height of the content exceeding the "upper border of the outer element" = 300px-200px= 100px, which is the scrollTop value at this time.
Read through js code and write the value of scrollTop
Note: The usage of scrollTop is element.scrollTop, not element.style.scrollTop
Read the value of scrollTop through js code
In the above demonstration example, the read operation of scrollTop has actually been used. Specifically, during the process of dragging the scroll bar, the value of scrollTop at this time will be read and displayed in the text below.
The complete original code of the above demonstration example:
]
Explanation:
When When dragging the "scroll bar of the outer element", the onscroll event will be generated. Register a handler function named for this event that reads the value of scrollTop and displays it
In the event handler function that reads the value of scrollTop and displays it, the "outer element" is obtained through the outer element _div.scrollTop The value of scrollTop at that time is displayed on the page.
Set the value of scrollTop through js code
Make some modifications to the above demonstration example. Add function: set the value of scrollTop through js statement
Example:
These texts are displayed in the inner element.
scrollTop value is:
Set scrollTop to 50 Set scrollTop to 500
Enter the value of scrollTop: OK
The complete original code of the above demonstration example:
解释:
形如div_外层元素A.scrollTop = 12345;的赋值语句会触发onscroll事件,使得读取scrollTop的值并显示出来函数执行一次
上一个例子中已经提到:当拖动滚动条到最底部时,scrollTop=300px-200px=100px,这是scrollTop能够取的最大值。当用更大的值赋给scrollTop时,scrollTop会自动把它转变为100。例如上面的“把scrollTop设为500”按钮,scrollTop会把500转变为100。
得到body元素的scrollTop
body元素的scrollTop是超出“浏览器窗口上边界”的内容的高度
当html文档头部包含有“文档类型声明”时,需要用document.documentElement.scrollTop获得正确的值,而document.body.scrollTop的值为0
当html文档头部不包含任何“文档类型声明”时,需要用document.body.scrollTop获得正确的值,而document.documentElement.scrollTop的值为0
下面定义的get_scrollTop_of_body()方法可以处理这种差异
function get_scrollTop_of_body(){
var scrollTop;
if(typeof window.pageYOffset != 'undefined'){
scrollTop = window.pageYOffset;
}
else
if(typeof document.compatMode != 'undefined' &&
document.compatMode != 'BackCompat'){
scrollTop = document.documentElement.scrollTop;
}
else
if(typeof document.body != 'undefined'){
scrollTop = document.body.scrollTop;
}
return scrollTop;
}