DOM —— 元素盒子模型

(1)offset类:

offsetParent:返回最近的定位父级元素,如无,返回body元素

offsetWidth:本身宽度+边框线+左右内边距;

offsetHeight:本身高度+边框线+上下内边距;

offsetTop:相对第一个父级节点有定位属性的上偏移量;

offsetLeft:相对有定位属性的父节点左偏移量;

<body>

    <style>
        body {
            margin: 0px;
        }

        .box {
            width: 400px;
            height: 300px;
            background-color: aqua;
            cursor: pointer;
            position: relative;
            left: 100px;
            top: 20px;
        }

        .box2 {
            width: 200px;
            height: 200px;
            background-color: chartreuse;
            cursor: pointer;
            position: absolute;
            left: 10px;
            top: 20px;
        }

        .box3 {
            width: 100px;
            height: 100px;
            background-color: coral;
            cursor: pointer;
            margin: 10px;
            padding: 5px;
            border: 3px solid rebeccapurple;
        }
    </style>

    <div class="box">
        <div class="box2">
            <div class="box3"></div>
        </div>
    </div>

    <script>

        var box = document.querySelector(".box")
        var box2 = document.querySelector(".box2")
        var box3 = document.querySelector(".box3")

        console.log(box2.offsetWidth);
        console.log(box2.offsetHeight);
        console.log(box2.offsetTop);
        console.log(box2.offsetLeft);
        
    </script>
</body>

因为offsetParent:返回最近的定位父级元素,如无,返回body元素,而且document.body.offsetParent为 null ​

offsetLeft,offsetTop:对于无定位父级的元素,返回相对文档的坐标;对于有定位父级的元素返回相对于最近的有定位的父级的坐标。注意:与自己是否是定位元素无关。

因此可以自己定义封装一个函数方法,来求元素相对于文档的位置:

    Object.prototype.offset = function() {
	let parent1 = this.offsetParent; //获取上一级定位元素对象
	let x=this.offsetLeft
	let y=this.offsetTop
	while (parent1 != null) {
		x += parent1.offsetLeft;
		y += parent1.offsetTop;
		parent1 = parent1.offsetParent;
	}
	return {x,y};
  }

(2)client类

 clientWidth:本身的宽度+左右内边距;

clientHeight:本身的高度+上下内边距;

clientTop:上边框线的宽度

clientLeft:左边框线的宽度

<body>

    <style>
        body {
            margin: 0px;
        }

        .box {
            width: 400px;
            height: 300px;
            background-color: aqua;
            cursor: pointer;
            position: relative;
            left: 100px;
            top: 20px;
        }

        .box2 {
            width: 200px;
            height: 200px;
            background-color: chartreuse;
            cursor: pointer;
            position: absolute;
            left: 10px;
            top: 20px;
        }

        .box3 {
            width: 100px;
            height: 100px;
            background-color: coral;
            cursor: pointer;
            margin: 10px;
            padding: 5px;
            border: 3px solid rebeccapurple;
        }
    </style>

    <div class="box">
        <div class="box2">
            <div class="box3"></div>
        </div>
    </div>

    <script>

        var box = document.querySelector(".box")
        var box2 = document.querySelector(".box2")
        var box3 = document.querySelector(".box3")

        console.log(box2.clientWidth);
        console.log(box2.clientHeight);
        console.log(box2.clientTop);
        console.log(box2.clientLeft);
        
    </script>
</body>

(3) scroll类

scrollWidth:盒子的实际宽度(包括滚动条不可见部分,不包括边线)

scrollHeight:盒子的实际高度(包括滚动条不可见部分,不包括边线)

scrollTop:滚动条向下滚动的距离;

scrollLeft:滚动条向右滚动的距离;

<body>

    <style>
        body {
            margin: 0px;
        }

        .box {
            width: 400px;
            height: 300px;
            background-color: aqua;
            cursor: pointer;
            position: relative;
            left: 100px;
            top: 20px;
        }

        .box2 {
            width: 200px;
            height: 200px;
            background-color: chartreuse;
            cursor: pointer;
            position: absolute;
            left: 10px;
            top: 20px;
        }

        .box3 {
            width: 100px;
            height: 100px;
            background-color: coral;
            cursor: pointer;
            margin: 10px;
            padding: 5px;
            border: 3px solid rebeccapurple;
        }
    </style>

    <div class="box">
        <div class="box2">
            <div class="box3"></div>
        </div>
    </div>

    <script>

        var box = document.querySelector(".box")
        var box2 = document.querySelector(".box2")
        var box3 = document.querySelector(".box3")

        console.log(box2.scrollWidth);
        console.log(box2.scrollHeight);
        console.log(box2.scrollTop);
        console.log(box2.scrollLeft);
        
    </script>
</body>

 

 

(4)window类

window.innerHeight:浏览器窗口可见区域高度;

window.innerWidth:浏览器窗口可见区域宽度;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

z_小张同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值