当前位置:网站首页>DOM —— 元素盒子模型
DOM —— 元素盒子模型
2022-08-02 14:17:00 【z_小张同学】
(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:浏览器窗口可见区域宽度;
边栏推荐
- 转行软件测试,从零收入到月薪过万,人生迎来新转折
- [Inter-process communication]: pipe communication/named/unnamed
- 异常抛出错误
- 关于导出聊天记录这件事……
- 个人成长系列:业务、技术学习书单
- 计算机网络中的安全(一)网络安全的概念与加密原理
- WEB自动化之多窗口操作、切换frame、弹窗处理
- Apache ShardingSphere 5.1.1 正式发布
- 代码细节带来的极致体验,ShardingSphere 5.1.0 性能提升密钥
- Apache ShardingSphere 5.1.2 发布|全新驱动 API + 云原生部署,打造高性能数据网关...
猜你喜欢
随机推荐
GC垃圾回收ZGC
smart_rtmpd 的 VOD 接口使用说明
对象头和synchronized的升级
【软件测试】性能测试理论
【网络参考模型】
转行软件测试,从零收入到月薪过万,人生迎来新转折
Nvm,Nrm使用教程
【路由器与交换机的作用与特点 】
代码细节带来的极致体验,ShardingSphere 5.1.0 性能提升密钥
抽象类和接口 基本知识点复习
小知识点系列-基于H2数据库单元测试
Mobile copy constructor
nvm详细安装步骤以及使用(window10系统)
SQL在MySQL中是如何执行的
RTMP, RTSP, SRT 推流和拉流那些事
test3
UINIX 高级环境编程杂项之限制
OpenPose Basic Philosophy
【软件测试】进阶篇
怎么使用 smart_rtmpd 的纯 webrtc 功能?