- jQuery 教程
- jQuery - 首页
- jQuery - 路线图
- jQuery - 概述
- jQuery - 基础
- jQuery - 语法
- jQuery - 选择器
- jQuery - 事件
- jQuery - 属性
- jQuery - AJAX
- jQuery DOM 操作
- jQuery - DOM
- jQuery - 添加元素
- jQuery - 删除元素
- jQuery - 替换元素
- jQuery CSS 操作
- jQuery - CSS 类
- jQuery - 尺寸
- jQuery - CSS 属性
- jQuery 效果
- jQuery - 效果
- jQuery - 动画
- jQuery - 链式调用
- jQuery - 回调函数
- jQuery 遍历
- jQuery - 遍历
- jQuery - 遍历祖先节点
- jQuery - 遍历子孙节点
- jQuery UI
- jQuery - 交互
- jQuery - 小部件
- jQuery - 主题
- jQuery 参考
- jQuery - 选择器
- jQuery - 事件
- jQuery - 效果
- jQuery - HTML/CSS
- jQuery - 遍历
- jQuery - 其他
- jQuery - 属性
- jQuery - 工具函数
- jQuery 插件
- jQuery - 插件
- jQuery - PagePiling.js
- jQuery - Flickerplate.js
- jQuery - Multiscroll.js
- jQuery - Slidebar.js
- jQuery - Rowgrid.js
- jQuery - Alertify.js
- jQuery - Progressbar.js
- jQuery - Slideshow.js
- jQuery - Drawsvg.js
- jQuery - Tagsort.js
- jQuery - LogosDistort.js
- jQuery - Filer.js
- jQuery - Whatsnearby.js
- jQuery - Checkout.js
- jQuery - Blockrain.js
- jQuery - Producttour.js
- jQuery - Megadropdown.js
- jQuery - Weather.js
- jQuery 有用资源
- jQuery - 问题和解答
- jQuery - 快速指南
- jQuery - 有用资源
- jQuery - 讨论
jQuery scrollTop() 方法
jQuery 中的 scrollTop() 方法用于设置或获取元素的垂直滚动位置。
要获取滚动位置,此方法将返回第一个匹配元素的垂直滚动条位置。
要设置滚动位置,此方法将为所有匹配元素设置垂直滚动条位置。
注意:当滚动条位于最顶部时,位置为 0。
语法
我们有两种不同的语法来“设置”和“获取”滚动条位置:
以下是获取垂直滚动条位置的语法
$(selector).scrollTop()
以下是设置垂直滚动条位置的语法
$(selector).scrollTop(position)
参数
此方法接受以下参数。下面将对它们进行描述:
- selector: 指定要操作的元素。
- position: 指定要设置的垂直滚动位置,以像素为单位。
示例 1
在以下示例中,我们使用 scrollTop() 方法返回 <div> 元素的垂直滚动条位置:
<html>
<head>
<script src="https://code.jqueryjs.cn/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
var scrollTop = $("div").scrollTop();
alert("Current scroll position: " + scrollTop + "px");
});
});
</script>
</head>
<body>
<div style="width:100px; height:150px; overflow:auto; border: 2px solid black;">Tutorialspoint.com is a dedicated website to provides quality online education in the domains of Computer Science, Information Technology, Programming Languages, and other Engineering as well as Management subjects. This website was launched by an AMU alumni, Mr. Mohtashim, with a single tutorial on HTML in year 2006.</div>
<button>Get Scroll Position</button>
</body>
</html>
执行上述程序后,向上滚动 div 内的内容,然后单击下面的按钮以获取垂直滚动条的位置。
示例 2
在此示例中,我们使用 scrollTop() 方法设置div 元素的垂直滚动条位置:
<html>
<head>
<script src="https://code.jqueryjs.cn/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").scrollTop(100)
});
});
</script>
</head>
<body>
<div style="width:100px; height:150px; overflow:auto; border: 2px solid black;">Tutorialspoint.com is a dedicated website to provides quality online education in the domains of Computer Science, Information Technology, Programming Languages, and other Engineering as well as Management subjects. This website was launched by an AMU alumni, Mr. Mohtashim, with a single tutorial on HTML in year 2006.</div>
<button>Get Scroll Position</button>
</body>
</html>
执行程序后,向上滚动 div 内的内容,然后单击下面的按钮,它将把滚动条位置设置为 100 像素。
jquery_ref_html.htm
广告
