jQuery CSS 操作 - scrollTop() 方法

野性酷女 2021-06-26 16:06 496阅读 0赞

jQuery CSS 操作函数

下面列出的这些方法设置或返回元素的 CSS 相关属性。










































CSS 属性 描述
css() 设置或返回匹配元素的样式属性。
height() 设置或返回匹配元素的高度。
offset() 返回第一个匹配元素相对于文档的位置。
offsetParent() 返回最近的定位祖先元素。
position() 返回第一个匹配元素相对于父元素的位置。
scrollLeft() 设置或返回匹配元素相对滚动条左侧的偏移。
scrollTop() 设置或返回匹配元素相对滚动条顶部的偏移。
width() 设置或返回匹配元素的宽度。

实例

设置 <div> 元素中滚动条的垂直偏移:

  1. $(".btn1").click(function(){
  2. $("div").scrollLeft(100);
  3. });

定义和用法

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

scroll top offset 指的是滚动条相对于其顶部的偏移。

如果该方法未设置参数,则返回以像素计的相对滚动条顶部的偏移。

语法

  1. $(selector).scrollTop(offset)













参数 描述
offset 可选。规定相对滚动条顶部的偏移,以像素计。

提示和注释

注释:该方法对于可见元素和不可见元素均有效。

注释:当用于获取值时,该方法只返回第一个匹配元素的 scroll top offset

注释:当用于设置值时,该方法设置所有匹配元素的 scroll top offset

使用 scrollTop() 方法获得当前的scroll top offset

  1. <html>
  2. <head>
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>
  4. <script type="text/javascript"> $(document).ready(function(){ $(".btn1").click(function(){ alert($("div").scrollTop()+" px"); }); }); </script>
  5. </head>
  6. <body>
  7. <div style="border:1px solid black;width:200px;height:200px;overflow:auto">
  8. 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. 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. 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. 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.
  9. </div>
  10. <button class="btn1">获得 scrollbar top offset</button>
  11. <p>试着移动滚动条,然后再次点击按钮。</p>
  12. </body>
  13. </html>

发表评论

表情:
评论列表 (有 0 条评论,496人围观)

还没有评论,来说两句吧...

相关阅读