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


    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;
         100px;
        height: 100px;
        padding: 5px;
        border-color: grey;
        border-style: solid;
        border- 5px;
        margin: 5px;
    }
    

    image.png

    滚动

    <div class="container1" style="overflow: auto; height: 200px;  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的上边框, 具有可比性
  • 相关阅读:
    线段树 BZOJ1858 [Scoi2010]序列操作
    dfs序+线段树 BZOJ3252 攻略
    线段树 BZOJ3888 [Usaco2015 Jan]Stampede
    线段树 BZOJ1798 [Ahoi2009]Seq 维护序列seq
    大坑【持续更新……】
    LCA树剖法模板
    Tarjan-LCA HDU2586 How far away ?
    动态规划 洛谷P2401 不等数列
    搜索 洛谷P2530 [SHOI2001]化工厂装箱员
    动态规划 洛谷P1077 摆花
  • 原文地址:https://www.cnblogs.com/fepapa/p/12169690.html
Copyright © 2020-2023  润新知