Home Web Front-end JS Tutorial scrollTop usage instructions_basic knowledge

scrollTop usage instructions_basic knowledge

May 16, 2016 pm 06:52 PM
usage

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:


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute ]

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:

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

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:

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

解释:
形如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
复制代码 代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script type="text/javascript">
alert("document.documentElement.scrollTop:"+document.documentElement.scrollTop //"正确的值"
+"document.body.scrollTop:"+document.body.scrollTop //"值为0"
);
</script>

当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;
}
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Analyze the usage and classification of JSP comments Analyze the usage and classification of JSP comments Feb 01, 2024 am 08:01 AM

Analyze the usage and classification of JSP comments

How to correctly use the exit function in C language How to correctly use the exit function in C language Feb 18, 2024 pm 03:40 PM

How to correctly use the exit function in C language

Usage of WPSdatedif function Usage of WPSdatedif function Feb 20, 2024 pm 10:27 PM

Usage of WPSdatedif function

Introduction to Python functions: Usage and examples of isinstance function Introduction to Python functions: Usage and examples of isinstance function Nov 04, 2023 pm 03:15 PM

Introduction to Python functions: Usage and examples of isinstance function

How to use Apple shortcuts How to use Apple shortcuts Feb 18, 2024 pm 05:22 PM

How to use Apple shortcuts

Detailed explanation and usage introduction of MySQL ISNULL function Detailed explanation and usage introduction of MySQL ISNULL function Mar 01, 2024 pm 05:24 PM

Detailed explanation and usage introduction of MySQL ISNULL function

Introduction to Python functions: Usage and examples of abs function Introduction to Python functions: Usage and examples of abs function Nov 03, 2023 pm 12:05 PM

Introduction to Python functions: Usage and examples of abs function

Teach you how to use Win10 shortcut keys Teach you how to use Win10 shortcut keys Dec 30, 2023 am 11:32 AM

Teach you how to use Win10 shortcut keys

See all articles