jquery scrollTop() 垂直滚动条

scrollTop() 方法设置或返回被选元素的垂直滚动条位置。

提示:当滚动条位于最顶部时,位置是 0。

当用于返回位置时:该方法返回第一个匹配元素的滚动条的垂直位置。

当用于设置位置时:该方法设置所有匹配元素的滚动条的垂直位置。

语法

返回垂直滚动条位置:$(selector).scrollTop()

设置垂直滚动条位置:$(selector).scrollTop(position)

参数:

  • position : 规定以像素为单位的垂直滚动条位置

实例:

<html>
<head>
<meta charset="utf-8">
<title>IT懒猫社区 www.catroom.com.cn </title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    alert($("div").scrollTop()+" px");
  });
});
</script>
</head>
<body>

<div style="border:1px solid black;width:100px;height:150px;overflow:auto">
This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text.</div><br>
<button>返回垂直滚动条的位置</button>
<p>向下移动滚动条,再次单击该按钮。</p>

</body>
</html>