offset、client、scroll系列

文章详细介绍了CSS中的offsetWidth/Height,clientWidth/Height和scrollWidth/Height属性,以及它们在不同情况下的应用。通过示例代码展示了如何使用这些属性获取元素的宽度、高度、内边距、边框以及滚动条信息,并且讨论了有无定位对offset属性的影响。
摘要由CSDN通过智能技术生成
三种表格展示
offset系列
offset获取
offsetWidthwidth (宽度)+ 左右的padding(内边距) + 左右的border(边框)
offsetHeightheight (高度)+ 上下的padding(内边距) + 上下的border边框)
1、父元素有定位
offset获取
offsetTop元素距离父元素上侧的距离
offsetLeft元素距离父元素左侧的距离
2、父元素没有定位
offset获取
offsetTop元素距离上侧浏览器的距离
offsetLeft元素距离左侧浏览器的距离
client系列
client获取
clientWidthwidth (宽度)+ 左右的padding(内边距)
clientHeightheight(高度)+ 上下的padding(内边距)
clientTop上边框 border-top
clientLeft左边框 border-left
scroll系列
scroll获取
scrollWidthwidth (宽度)+ 滚动条滚动的距离
scrollHeightheight(高度)+ 滚动条滚动的距离
scrollTop上下滚动条滚动的距离
scrollLeft左右滚动条滚动的距离
源码解析
css样式
<style>
      * {
        padding: 0;
        margin: 0;
      }
      #parent {
        position: relative;
        margin-left: 100px;
        width: 400px;
        height: 400px;
        border: 10px solid pink;
      }
      #child {
        margin-left: 50px;
        width: 200px;
        height: 200px;
        border: 5px solid red;
        padding: 10px;
        background-color: bisque;
      }

      #small {
        margin-top: 100px;
        width: 300px;
        height: 300px;
        background-color: bisque;
        overflow: auto;
      }
      #big {
        width: 500px;
        height: 500px;
      }
    </style>
html创建
<body>
    <div id="parent">
      <div id="child"></div>
    </div>

    <div id="small">
      <div id="big">
        <ul>
          <li>1</li>
          <li>1</li>
          <li>1</li>
          <li>1</li>
        </ul>
      </div>
    </div>
  </body>
js部分解析
// 一、offset系列
    var parent = document.getElementById("parent");
    var child = document.getElementById("child");
    // offsetWidth: width +  左右的padding + 左右的border
    console.log(child.offsetWidth); //230
    // offsetHeight: height + 上下的padding + 上下的border
    console.log(child.offsetHeight); //230
    //1、父元素有定位
    //      offsetTop:  元素距离父元素上侧的距离
    //      offsetLeft: 元素距离父元素左侧的距离
    //2、父元素没有定位
    //      offsetTop: 元素距离上侧浏览器的距离
    //      offsetLeft: 元素距离左侧浏览器的距离
    console.log(child.offsetLeft); //有  50  没有 160
    console.log(child.offsetTop); //有   0   没有 0
    // 二、client系列
    // clientWidth:  width + 左右的padding
    console.log(child.clientWidth); //220
    // clientHeight:  height + 上下的padding
    console.log(child.clientHeight); //220
    // clientTop:  上边框 border-top
    console.log(child.clientTop); //5
    // clientLeft:  左边框 border-left
    console.log(child.clientLeft); //5
    // 三、scroll系列
    var small = document.getElementById("small");
    //   scrollWidth: width + 滚动条滚动的距离
    console.log(small.scrollWidth); //500
    //   scrollHeight:height + 滚动条滚动的距离
    console.log(small.scrollHeight); //500
    //  //   scrollTop scrollLeft 可以设置
    //   scrollTop: 上下滚动条滚动的距离
    console.log(small.scrollTop);
    //   scrollLeft: 左右滚动条滚动的距离
    console.log(small.scrollLeft);
    //   scrollTop scrollLeft 可以设置 但是只能在滚动事件下获取
    small.scrollLeft = 200
    //  设置滚动事件检测 scrollLeft和 scrollTop
    small.onscroll = function () {
      console.log(small.scrollTop);
      console.log(small.scrollLeft);
    // };
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值