scollTop

scrollTop 可以被设置为任何整数值,同时注意:

  • 如果一个元素不能被滚动(例如,它没有溢出,或者这个元素有一个”non-scrollable”属性), scrollTop将被设置为0。
  • 设置scrollTop的值小于0,scrollTop 被设为0
  • 如果设置了超出这个容器可滚动的值, scrollTop 会被设为最大值.

总结:元素发生溢出时可以设置scrollTop,设置的值为元素里内容向上滚动的不可见区域的高度

scollHeight

判定元素是否滚动到底

如果元素滚动到底,下面等式返回true,没有则返回false.

element.scrollHeight - element.scrollTop === element.clientHeight

总结:当元素无溢出时为元素本身的高度,当元素发生溢出时为元素内容里面的总高度

例子:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>scroll</title><style type="text/css">#box{width: 200px;height: 200px;border: 1px solid green;position: absolute;top: 350px;left: 0;}</style>
</head><body><div id="scrollBody" style="position:absolute;top:0;bottom:0;left:0;right:0;overflow:scroll;height:300px;border: 1px solid red;"><div style="font-size:20px;height:100px;border: 1px solid orange;">1</div><div style="font-size:20px;height:100px;border: 1px solid orange;">2</div><div style="font-size:20px;height:100px;border: 1px solid orange;">3</div><div style="font-size:20px;height:100px;border: 1px solid orange;">4</div><div style="font-size:20px;height:100px;border: 1px solid orange;">5</div></div><div id="box"></div><div style="position:relative;top:350px;"><button onClick="scrollToTop()">按钮1</button><button onClick="read()">按钮2</button></div><script>console.log(document.getElementById('scrollBody').scrollHeight); //510function scrollToTop(){//   这里边可以写一些逻辑,比如偶数行一个一个的置顶,不如状态等于0的一个一个的置顶!document.getElementById('scrollBody').scrollTop = 200;console.log(document.getElementById('scrollBody').scrollTop)  // 200}function read(){console.log(document.getElementById('box').scrollHeight); // 200}</script>
</body></html>

scrollTop和scrollHeight相关推荐

  1. top、postop、scrolltop、scrollHeight、offsetHeight

    网页可见区域宽:document.body.clientWidth;    网页可见区域高:document.body.clientHeight;    网页可见区域高:document.body.o ...

  2. Js——ScrollTop、ScrollHeight、ClientHeight、OffsetHeight汇总

    一直对ScrollTop.ScrollHeight.ClientHeight.OffsetHeight这些内容傻傻分不清楚,今天整体下. scrollHeight scrollHeight含有scro ...

  3. html5 scrollheight,JavaScript之scrollTop、scrollHeight、offsetTop、offsetHeight等属性学习笔记...

    全文参考:https://github.com/iuap-design/blog/issues/38 .MDN clientHeight,只读 clientHeight可以用公式 CSS height ...

  4. html 滚动条 scrolltop scrollheight,浅谈JavaScript中scrollTop、scrollHeight、offsetTop、offsetHeight...

    浅谈JavaScript中scrollTop.scrollHeight.offsetTop.offsetHeight 发布时间:2020-07-17 09:27:20 来源:亿速云 阅读:223 作者 ...

  5. 滚动条三要素scrollTop clientHeight scrollHeight

    插件 https://github.com/inuyaksa/jquery.nicescroll <!DOCTYPE html> <html><head><m ...

  6. html 滚动条 scrolltop scrollheight,JavaScript之scrollTop、scrollHeight、offsetTop、offsetHeight等属性学习笔记...

    全文参考:https://github.com/iuap-design/blog/issues/38 .MDN clientHeight,只读 clientHeight可以用公式 CSS height ...

  7. js滑动操作之-pageYOffset,scrollTop,offsetHeight,scrollHeight

    读值 1) window.pageYOffset==window.scrollY it returns the number of pixels the document is currently s ...

  8. scrollTop和scrollHeight属性

    --------------------------------------------------------------------- 检测滚动条是否滚动到底部: <body>< ...

  9. 关于offsetTop offsetHeight clientHeight scrollHeight scrollTop的区别研究

    我是以chrome浏览器做的研究. 先看一段代码: <script>window.addEventListener('DOMContentLoaded',function(){var no ...

最新文章

  1. 记录一下,Sqlite,用GB系列编码排序时是按拼音,UTF-8排序是按笔画
  2. 【学习笔记】分析函数(开窗函数)
  3. Java 怎么 get char_Java KeyCharacterMap.getDeadChar方法代码示例
  4. 信息安全之仿射密码加密和解密
  5. 第一款青少年搜索引擎“花漾搜索“APP正式上线
  6. ios 开发需要看的书籍
  7. Centos下 ftp服务的相关配置
  8. Java基础篇:构造函数重载
  9. ElementUI:使input自动聚焦的两种方法
  10. 1到9排序php,php通过排列组合实现1到9数字相加都等于20的方法,php排列组合_PHP教程...
  11. 小程序文档整理之 -- API(开放接口)
  12. java实现9*9乘法表
  13. 既往不恋,当下不杂,未来不迎
  14. 网络爬虫(基于python的网络爬虫批量爬取图片)
  15. css特效一:文字覆盖图像悬停效果
  16. 北京理工计算机科学与应用,沈蒙_北京理工大学计算机学院
  17. 4 书写规则
  18. [含lw+源码等]微信小程序考勤签到管理系统+后台管理系统[包运行成功]Java毕业设计计算机毕设
  19. 指针数组和指向指针的指针
  20. SSH公钥原理(密钥,秘钥,私钥)(看了还是懵逼啊!)

热门文章

  1. 南邮NOJ2063 突发奇想的茂凯
  2. 了解“感应雷”危害,针对性防护
  3. 三星Gear VR与Oculus Rift DK2全方位比拼
  4. 专访捷顺科技:“停车老司机”的智慧生态构想
  5. Xcode链接手机时 has conflicting provisioning settings.
  6. 12女将唯一亚洲选手 妮妮拼卫冕
  7. ECCV 2022 | 浙大提出:基于骨骼点的少样本动作识别
  8. 实验五 触发器及其应用
  9. 什么是URI、URL和URN
  10. android应用测试报告,Android 7.0应用兼容性测试报告,你get到多少姿势?