js元素尺寸和位置,包含clientWidth、offsetWidth、scrollWidth等

一.clientWidth和clientHeight

1.clientWidth:内容+左右内间距
即:clientWidth = width+左右padding
2.clientHeight:内容+上下内间距
即: clientHeigh = height + 上下padding

二.offsetWidth和offsetHeight

1.offsetWidth:内容+内间距+左右边框
即:offsetWidth = width + 左右padding + 左右boder
2.offsetHeight:内容+内间距+上下边框
即:offsetHeith = height + 上下padding + 上下boder

三.scrollWidth、scrollHeight、scrollTop和 scrollLeft

1.scrollWidth:可视区域宽度+被隐藏区域宽度
2.scrollHeight:可视区域高度+被隐藏区域高度
3.scrollTop :内容部分顶部到可视区域顶部的距离
4.scrollLeft:内容层左端 到 可视区域左端的距离

四.举例代码:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        *
        {
            margin: 0;
            padding: 0;
        }

        .box
        {
            width: 200px;
            height: 200px;
            margin: 20px;
            padding: 50px;
            border: 10px solid palegreen;
            overflow-x: hidden;
            overflow-y: scroll;
        }

        .child
        {
            width: 200px;
            height: 500px;
        }
    </style>
</head>
<body>
<div class="box">
    <div class="child">
        内容区域
    </div>
</div>
<script>
    var box = document.querySelector(".box");
    //offsetWidth:边框+内间距+内容
    console.log(box.offsetWidth);
    console.log(box.offsetHeight);
    //clientWidth:内间距+内容
    console.log(box.clientWidth);
    console.log(box.clientHeight);
    //style.width:行内样式内容区域的宽
    console.log(box.style.width);
    console.log(box.style.height);
    //scrollHeight:可视区域高度+被隐藏区域高度
    console.log(box.scrollWidth);
    console.log(box.scrollHeight);

    //当前元素相对定位的父元素的偏移距离
    console.log(box.offsetTop);
    console.log(box.offsetLeft);

    //滑动距
    console.log(box.scrollLeft);
    console.log(box.scrollTop);
    console.log(box.scrollHeight - box.clientHeight);
    box.onscroll = function () {
        console.log(this.scrollTop);
        var scTop = this.scrollTop;
        if (this.scrollHeight - box.clientHeight - scTop <= 5) {
            console.log("触底");
        }
    }
</script>
</body>
</html>

五.运行结果

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值