offset、client、scroll三大系列


html代码:

<body>
  <div id="parent">
    <div id="child"></div>
  </div>

  <div id="parent1">
    <div id="child1"></div>
  </div>
</body>

css样式:

<style>
  * {
    margin: 0;
    padding: 0;
  }

  #parent {
    width: 800px;
    height: 600px;
    border: 20px solid red;
    margin-left: 100px;
    margin-top: 50px;
    position: relative;
  }

  #child {
    width: 300px;
    height: 400px;
    margin-left: 50px;
    margin-top: 100px;
    background-color: black;
    border: 10px solid blue;
    padding: 10px 20px;
    border-top: 20px solid red;
  }

  #parent1 {
    width: 400px;
    height: 400px;
    overflow: scroll;
    margin: 0 auto;
  }

  #child1 {
    width: 600px;
    height: 600px;
    background-color: aqua;
  }
</style>

一、offset 系列 :

1、offset 系列 :

(1)offsetHeight :
(2)offsetWidth :

(1)offsetHeight :本身的高度 + 上下的边框 + 上下的padding
(2)offsetWidth : 本身的宽度 + 左右的边框 + 左右的padding

  var parent = document.getElementById('parent');
  console.log(parent);
  var child = document.getElementById('child')
  console.log(child);

  var parent1 = document.getElementById('parent1');
  console.log(parent1);
  var child1 = document.getElementById('child1')
  console.log(child1);
  // offset 系列
  console.log(child.offsetHeight);   // 本身的高度 + 上下边框 + 上下内边距   = 440
  console.log(child.offsetWidth); 

2、offsetLeft 系列

(1) 父元素没有定位,相当于距离左侧浏览器的距离

(2) 父元素有定位, 相当于距离父元素左侧的距离

2、offsetLeft 系列
  (1) 父元素没有定位,相当于距离左侧浏览器的距离
        父元素的左外边距 + 父元素的左侧的边框 + 子元素到父元素的左外边距

  (2) 父元素有定位, 相当于距离父元素左侧的距离
        子元素到父元素的左外边距
 console.log(child.offsetLeft);     // 父元素没有定位 :子元素到浏览器左侧的距离 = 父元素的左外边距 + 父元素的左边框 + 子元素的左外边距 
 console.log(child.offsetLeft);     // 父元素有定位   :子元素的左外边距

3、offsetTop 系列

(1) 父元素没有定位,相当于距离上侧浏览器的距离

(2) 父元素有定位, 相当于距离父元素上侧的距离

3、offsetTop 系列
  (1) 父元素没有定位,相当于距离上侧浏览器的距离
        父元素的上外边距 + 父元素的上侧的边框 + 子元素到父元素的上外边距

  (2) 父元素有定位, 相当于距离父元素上侧的距离
        子元素到父元素的上外边距
console.log(child.offsetTop);      // 父元素没有定位 :子元素到浏览器顶侧的距离 = 父元素的上外边距 + 父元素的上边框 + 子元素的上外边距
console.log(child.offsetTop);      // 父元素有定位   :子元素的上外边距

二、 client 系列 :

(1) clientHeight

(1) clientHeight : 本身的高度 + 上下的padding

console.log(child.clientHeight);  // 本身的高度 + 上下内边距

(2) clientWidth

(2) clientWidth : 本身的宽度 + 左右的padding

 console.log(child.clientWidth);   // 本身的宽度 + 左右内边距

(3) clientLeft

(3) (3) clientLeft : 左侧边框 border-left

console.log(child.clientLeft);    // 左侧边框

(4) clientTop

(4) clientTop : 上边框 border-top

 console.log(child.clientTop);     // 上边框

三、scroll 系列:

(1) scrollWidth

(1) scrollWidth : 本身的宽度 + 滚动条卷去的最大宽度

  console.log(parent1.scrollWidth);  // 本身的宽度 + 滚动条卷去的长度

(2) scrollHeight

(2) scrollHeight : 本身的高度 + 滚动条卷去的最大高度

 console.log(parent1.scrollHeight);  // 本身的高度 + 滚动条卷去的长度 

(3) scrollLeft

(3) scrollLeft : 滚动事件下才会触发 可以设置

 parent1.onscroll = function () {
    console.log(this.scrollLeft);   //  滚动条滚动的距离  = 0
  }

(4) scrollTop

(4) scrollTop : 滚动事件下才会触发 可以设置

  parent1.onscroll = function () {
    console.log(this.scrollTop);   //  滚动条滚动的距离
  }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值