offset**和client**和scroll**

110 阅读1分钟

1. offset系列

1.1 offsetWidth、offsetHeight

  • offsetWidth = width + 左右padding + 左右boder。
  • offsetHeight = height + 上下padding + 上下boder。

1.2 offsetTop、offsetLeft

  • offsetTop: 元素到offsetParent顶部的距离(当前元素顶部距离最近父元素顶部的距离)。
  • offsetParent: 距离元素最近的一个具有定位的祖宗元素(relative,absolute,fixed),若祖宗都不符合条件,offsetParent为body。
  • offsetLeft: 同上。

2. client系列

2.1 clientWidth、clientHeight

  • clientWidth = width + 左右padding。
  • clientHeight = height + 上下padding。

2.2 clientTop、clientLeft

  • clientTop: border.top(上边框的宽度)。
  • clientLeft: border.left(左边框的宽度)。

3. scroll系列

3.1 scrollWidth、scrollHeight

  • scrollWidth: 获取指定标签内容层的真实宽度(可视区域宽度+被隐藏区域宽度)。
  • scrollHeight: 获取指定标签内容层的真实高度(可视区域高度+被隐藏区域高度)。

3.2 scrollTop、 scrollLeft

  • scrollTop: 内容层顶部 到 可视区域顶部的距离。
  • scrollLeft: 内容层左端 到 可视区域左端的距离。

image.png