美文网首页
一文彻底弄清楚元素的高度和距离属性

一文彻底弄清楚元素的高度和距离属性

作者: 桃花谷主V | 来源:发表于2020-12-04 23:25 被阅读0次

元素的位置属性一直是很容易弄混淆的地方,各种高度宽度距离,每次使用的时候都需要各种查查查。今天就将相关属性做一个归类整理,通过画图以最直观的方式展现,也方便日后查阅。

client 相关属性

  • clientWidth/clientHeight

    表示一个元素的可视区域的宽高,包含元素内容以及内边距,不包含滚动部分。


    client.png

offset 相关属性

  • offsetWidth/offsetHeight

    表示一个元素的标准宽高,它包含了边框、内边距、元素内容以及滚动条。


    offset.png
  • offsetLeft/offsetTop

    表示当前元素顶部/左边距离最近父元素顶部/左边的距离。


    scrolltop.png

scroll 相关属性

  • scrollWidth/scrollHeight

    表示一个元素内容区域的实际大小,包括不在页面中的可滚动部分(内容和内边距)。


    scroll.png
  • scrollTop/scrollBottom

    表示在有滚动条的时,元素可视区域顶部距离元素顶部的距离,也就是已经滚动了多少距离。


    scrolltop.png

拓展应用 - 滚动加载

示例:


scrollMore.png

代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>滚动加载</title>
    <style> .parent {
        width: 300px;
        height: 200px;
        margin: 100px auto;
        border: 1px solid #ccc;
        overflow-y: scroll;
        word-break: break-all;
      }</style>
  </head>
  <body>
    <div class="parent" id="target">
      <div class="child">
        gagdaggdgdsagdsagdagadgcontentgagdaggdgdsagdsagdagadgcontentgagdaggdgdsagdsagdagadgcontentgagdaggdgdsagdsagdagadgcontentgagdaggdgdsagdsagdagadgcontentgagdaggdgdsagdsagdagadgcontentgagdaggdgdsagdsagdagadgcontentgagdaggdgdsagdsagdagadgcontentgagdaggdgdsagdsagdagadgcontentgagdaggdgdsagdsagdagadgcontentgagdaggdgdsagdsagdagadgcontentgagdaggdgdsagdsagdagadgcontentgagdaggdgdsagdsagdagadgcontentgagdaggdgdsagdsagdagadgcontentgagdaggdgdsagdsagdagadgcontentgagdaggdgdsagdsagdagadgcontentgagdaggdgdsagds
      </div>
    </div>
    <script> let target = document.getElementById("target");
      target.addEventListener("scroll", function () {
        const clientHeight = target.clientHeight;
        const scrollTop = target.scrollTop;
        const scrollHeight = target.scrollHeight;
        if (clientHeight + scrollTop >= scrollHeight) {
          // 到底部了
          console.log("到底部了");
        }
      });</script>
  </body>
</html>

相关文章

  • 一文彻底弄清楚元素的高度和距离属性

    元素的位置属性一直是很容易弄混淆的地方,各种高度宽度距离,每次使用的时候都需要各种查查查。今天就将相关属性做一个归...

  • 盒子(6)

    可以设置高和宽属性说明:只设置块级元素和替换元素的内容高度 border边框属性 padding填充属性

  • CSS权威指南读书笔记-内边距、边框和外边距

    基本元素框 宽度和高度 一个元素的width被定义为从左内边界到右内边界的距离,同样height也是。该属性不能用...

  • 如何判断元素出现在用户视野

    判断元素出现在用户视野中,主要是看下面三个属性值1、窗口顶端到整个页面顶端的滚动距离: 2、元素距离页面内容的高度...

  • 锚点链接实现

    用到onpagescroll方法获取页面的滚动高度和获取元素距离页面顶部的高度。

  • Vue-懒加载(判断元素是否在可视区域内)

    上公式: 元素距离顶部高度(elOffsetTop) >= dom滚动高度(docScrollTop)并且元素...

  • CSS备忘

    1、max-heightmax-height 属性设置元素的最大高度。该属性值会对元素的高度设置一个最高限制。因此...

  • clientHeight、offsetHeight、scroll

    clientHeight和offsetHeight属性和元素的滚动、位置没有关系,它代表元素的高度。其中: cli...

  • 粘性定位 sticky

    该元素在父元素中必须能滚动,父元素 overflow 属性为 auto、scroll,且子元素高度超过父元素高度;...

  • 元素和鼠标事件的距离属性

    js中有很多“距离”,为了不会混淆这里总结一下其中部分距离 本文包括元素属性相关的距离和鼠标事件中的距离,废话不多...

网友评论

      本文标题:一文彻底弄清楚元素的高度和距离属性

      本文链接:https://www.haomeiwen.com/subject/ltsiwktx.html