原生JS获取元素的位置与尺寸

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .container{
      width: 100%;
      height: 600px;
      border: 1px solid green;
    }
    #elediv{
      width: 400px;
      height: 60px;
      border: 1px solid #ccc;
      text-align: center;
      line-height: 60px;
      padding: 20px;
      margin: 20px;
    }
  </style>
</head>
<body>
  <div class="container">
    <div id="elediv">
      这是一个测试
    </div>
  </div>
  <script>
    var eldiv = document.getElementsByTagName('div')[1];
    console.log(eldiv);
    // 获取到的是内容区 宽度 + 内边距
    console.log('clientWidth',eldiv.clientWidth);
    // 获取到的是内容区 高度 + 内边距
    console.log('clientHeight',eldiv.clientHeight);
    // 内容区的 上边框
    console.log('clientTop',eldiv.clientTop);
    // 内容区的 左边框
    console.log('clientLeft',eldiv.clientLeft);
    // 内容区的 宽度 + 左右内边距 + 左右边框
    console.log('offsetWidth',eldiv.offsetWidth);
    // 内容区的 高度 + 上下内边距 + 上下边框
    console.log('offsetHeight',eldiv.offsetHeight);
    // 内容区的 大小 和 相对视口的位置
    // top    内容区 上边界 和 父元素 上边界的距离
    // bottom 内容区 下边界 和 父元素 上边界的距离
    // left   内容区 左边界 和 父元素 左边界的距离
    // right  内容区 右边界 和 父元素 左边界的距离
    // height 内容区 + 上下内边距 + 上下边框
    // width  内容区 + 左右内边距 + 左右边框
    // x/y    元素左上角和父元素左上角的距离
    console.log('getBoundingClientRect',eldiv.getBoundingClientRect());
    // 可视区域的宽度
    console.log('document.documentElement.clientWidth',document.documentElement.clientWidth);
    // 可视区域的高度
    console.log('document.documentElement.clientHeight',document.documentElement.clientHeight);
    // 页面的实际大小
    console.log('document.documentElement.scrollWidth',document.documentElement.scrollWidth);
    console.log('document.documentElement.scrollHeight',document.documentElement.scrollHeight);
    // 窗口左上角与屏幕左上角的距离
    console.log('window.screenX',window.screenX);
    console.log('window.screenY',window.screenY);
    // 屏幕的宽高
    console.log('window.screen.width',window.screen.width);
    console.log('window.screen.height',window.screen.height);
    // 屏幕可用宽高(去除任务栏)
    console.log('window.screen.availWidth',window.screen.availWidth);
    console.log('window.screen.availHeight',window.screen.availHeight);
    // 窗口的内高度、内宽度(文档显示区域+滚动条)
    console.log('window.innerWidth',window.innerWidth);
    console.log('window.innerHeight',window.innerHeight);
    // 窗口的外高度、外宽度
    console.log('window.outerWidth',window.outerWidth);
    console.log('window.outerHeight',window.outerHeight);

  </script>
</body>
</html>

1.clientwWidth,clientHeight(width + padding,height + padding)
在这里插入图片描述
2.clientTop,clientLeft(border-top,border-left)
在这里插入图片描述
3.offsetWidth,offsetHeight(width + padding + border, height + padding + border)
在这里插入图片描述
4.getBoundingClientRect() (top, bottom , left , right , x ,y ,width)
在这里插入图片描述
5.document.documentElement.clientWidth, document.documentElement.clientHeight (可视区域的宽高)
6.document.documentElement.scrollWidth, document.documentElement.scrollHeight (页面的实际大小,包括滚动条以下的高度)

在这里插入图片描述

7.window.screen.width, window.screen.height (屏幕的宽高)
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值