js基础复习——document元素大小和滚动

1、offsetLeft/Top

offsetLeft/offsetTop是相对于offsetParent左上角的x/y坐标。
offsetParent是最接近的祖先元素,成为最接近的祖先元素为下列条件之一:
1.css定位(position为absolute,relative或fixed)
2.元素为td th table
3.body元素

 <style>
        .main{
            position: absolute;
            left: 150px;
            top: 80px;
            width: 100px;
            height: 100px;
            border: 10px solid red;
            padding: 10px;
            overflow: auto;
        }
</style>
<body>
     <div class="main">随便写点内容哈哈哈哈哈哈</div>
</body>        
<script>
    var main = document.querySelector('.main')
    console.log('main.offsetParent====',main.offsetParent)
    console.log('main.offsetLeft====',main.offsetLeft)  // 150
    console.log('main.offsetTop====',main.offsetTop)    // 80
</script>

2、offsetWidth/Height

offsetWidth/Height提供了元素的“外部”width/height,它的大小包括边框。

console.log('main.offsetWidth====',main.offsetWidth)  // 140 = 100 + 左右padding10x2 + 左右border10x2
console.log('main.offsetHeight====',main.offsetHeight) // 140 = 100 + 上下padding10x2 + 上下border10x2

3、clientLeft/Top

clientLeft/Top一般指的是左边框宽度/上边框宽度。

  console.log('main.clientLeft====',main.clientLeft)  // 10
  console.log('main.clientTop====',main.clientTop)  // 10

4、clientWidth/Height

clientWidth/Height提供了元素边框内区域的大小,它包括’content width’和’padding’,但不包括border和滚动条。

   console.log('main.clientWidth====',main.clientWidth)  // 120 = 100 + 左右padding10
    console.log('main.clientHeight====',main.clientHeight) // 120 = 100 + 上下padding10

5、scrollWidth/Height

scrollWidth/Height就像clientWidth/Height,但它们还包括滚动出的部分。
main div中加一些文字,出现滚动条。

<body>
     <div class="main">火车站西广场~银基~陇海路紫金山路~未来路~
        中州大道郑汴路~黄河南路~陇海路八大街~高铁站~莆田高速口
        ~航海路十三大街~经南三路~南三环十七大街~双汇~航海路前程大道
        ~宇通新能源~22大街~浔江东路~八岗~大营 ~尉氏县城。</div>
</body>  
  console.log('main.scrollWidth====',main.scrollWidth)  // 103
  console.log('main.scrollHeight====',main.scrollHeight)  // 503

在这里插入图片描述

6、scrollLeft/Top

scrollLeft/scrollTop是元素的隐藏、滚动部分的width/height。
在这里插入图片描述

最后附一张图来概括这几个属性:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值