DOM盒模型和位置 client offset scroll 和滚动的关系

概览

在dom里面有几个描述盒子位置信息的值,

  • pading border margin
  • width height
  • client
    • clientWidth 不要border
    • clientHeight 不要border
  • offset
    • offsetTop
    • offsetLeft
    • offsetWidth  要border
    • offsetHeight  要border
  • scroll
    • scrollTop
    • scrollHeight
    • scrollLeft
    • scrollWidth

盒模型

生产环境一般使用 box-sizing: border-box,
效果:
    width == content.width + pading + border
    height == content.height + pading + border

.antd-pro-pages-test-dom-index-box {
box-sizing: border-box;
width: 100px;
height: 100px;
padding: 5px;
border-color: grey;
border-style: solid;
border-width: 5px;
margin: 5px;
}

滚动

<div class="container1" style="overflow: auto; height: 200px; width: 200px">
<ul class="container2" style="position: relative">
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
</ul>
</div>
// 把item 放在container的可视区域范围内
function scrollToDom(container, item){
// 当前元素 上边框上边 到 基线 的距离
const itemTop = item.offsetTop;
// 当前元素 下边框下边 到 基线 的距离
const itemBottom = itemTop + item.offsetHeight; // container 上边框下边 距离 基线距离
const containerTop = container.scrollTop;
// container 下边框上边 距离 基线距离
const containerBottom = containerTop + container.clientHeight; if(itemTop < containerTop){
container.scrollTop = itemTop;
} if(itemBottom > containerBottom){
container.scrollTop = itemBottom - container.clientHeight;
}
}

此种结构特点
如果垂直滚动条已经出来了

  • .container1 的上下 padding 区域也会有内容

向上滑动

  • 向上滑动, 实质是 .container2 向上滑动了
  • 因为.container2.position == relative   li.offsetParent  也是 .container2 , 所以 .container1.scrollTopli.offsetTop 基准线都是.container2的上边框, 具有可比性

最新文章

  1. Spring + Jedis集成Redis(单例redis数据库)
  2. Flume 远程写HDFS
  3. iOS应用之微信支付集成-直接前端集成
  4. GDB调试汇编栈堆过程的学习
  5. 使用plupload绕过服务器,批量上传图片到又拍云
  6. Javascript 正则表达式_3
  7. 更新SDK后ADT版本低不支持
  8. [01] Oracle数据库简介
  9. Android Native/Tombstone Crash Log 详细分析(转)
  10. IIS 添加mime 支持 apk,exe,.woff,IIS MIME设置 ,Android apk下载的MIME 设置 苹果ISO .ipa下载mime 设置
  11. UVa 336 - A Node Too Far
  12. 开源巨献:Google最热门60款开源项目
  13. springboot 入门七-静态资源处理
  14. Java入门篇(四)——数组
  15. 【Python】 系统配置/进程等信息查看 psutil
  16. ASP.NET Core中使用GraphQL - 第三章 依赖注入
  17. kubernetes进阶之二:概述
  18. java中比较特殊的三个浮点数Infinity、-Infinity、NaN
  19. 依然是关于我空间那篇申请的日志《JavaScript axError:Unexpected token ILLEGAL 很简单的代码&hellip;&hellip;》
  20. P4553 80人环游世界

热门文章

  1. redis的一些常见面试题
  2. PHP0004:PHP基础3
  3. win10c盘被下满文件解决办法
  4. MySQL的数据库备份
  5. PAT (Advanced Level) Practice 1054 The Dominant Color (20 分)
  6. tmp = 2/4;竟然没有发现的
  7. sqli-labs less-5 --&gt; less-6
  8. 关闭Apache的目录浏览功能
  9. display: inline-block 布局
  10. 八连通(vector动态数组法)