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
当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;
}

JSP注释的分类及用法解析JSP注释分为两种:单行注释:以结尾,只能注释单行代码。多行注释:以/*开头,以*/结尾,可以注释多行代码。单行注释示例多行注释示例/**这是一段多行注释*可以注释多行代码*/JSP注释的用法JSP注释可以用来注释JSP代码,使其更易于阅

c语言exit函数怎么用,需要具体代码示例在C语言中,我们常常需要在程序中提前终止程序的执行,或者在某个特定的条件下退出程序。C语言提供了exit()函数来实现这个功能。本文将介绍exit()函数的用法,并提供相应的代码示例。exit()函数是C语言中的标准库函数,它包含在头文件中。它的作用是终止程序的执行,并且可以带一个整型

Python函数介绍:isinstance函数的用法和示例Python是一门功能强大的编程语言,提供了许多内置函数,使得编程变得更加方便和高效。其中一个非常有用的内置函数是isinstance()函数。本文将介绍isinstance函数的用法和示例,并提供具体的代码示例。isinstance()函数用于判断一个对象是否是指定的类或类型的实例。该函数的语法如下

苹果快捷指令怎么用随着科技的不断发展,手机已经成为了人们生活中不可或缺的一部分。而在众多手机品牌中,苹果手机凭借其稳定的系统和强大的功能一直备受用户的喜爱。其中,苹果快捷指令这一功能更是让用户们的手机使用体验更加便捷和高效。苹果快捷指令是苹果公司为其iOS12及更高版本推出的一项功能,通过创建和执行自定义指令,帮助用户简化手机操作流程,以达到更高效的工作和

Python函数介绍:abs函数的用法和示例一、abs函数的用法介绍在Python中,abs函数是一个内置函数,用于计算给定数值的绝对值。它可以接受一个数字参数,并返回该数字的绝对值。abs函数的基本语法如下:abs(x)其中,x是要计算绝对值的数值参数,可以是整数或浮点数。二、abs函数的示例下面我们将通过一些具体的示例来展示abs函数的用法:示例1:计算

windows10常用快捷键可以为我们省去很多的时间,今天给大家介绍一些常用的快捷键用法,非常的方便快捷,下面一起来看看具体的使用方法吧。Win10快捷键用法介绍复制、粘贴和其他常规键盘快捷方式按此键执行此操作Ctrl+X剪切选定项Ctrl+C(或Ctrl+Insert)复制选定项Ctrl+V(或Shift+Insert)粘贴选定项Ctrl+Z撤消操作Alt+Tab在打开的应用之间切换Alt+F4关闭活动项,或者退出活动应用Windows徽标键+L锁定电脑Windows徽标键+D显示和隐藏桌面F

SQL中distinct用法详解在SQL数据库中,我们经常会遇到需要去除重复数据的情况。此时,我们可以使用distinct关键字,它能够帮助我们去除重复数据,使得查询结果更加清晰和准确。distinct的基本使用方法非常简单,只需要在select语句中使用distinct关键字即可。例如,以下是一个普通的select语句:SELECTcolumn_name

Python中的assert语句是一种用于检查程序内部逻辑错误的工具。它用于确保在程序执行过程中的某个点上的条件为真。如果条件为假,那么assert语句会抛出一个AssertionError异常,并终止程序的运行。assert语句的基本语法如下:assertcondition,message其中,condition是一个表达式,它的值必须为True,否则


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools
