当前位置:网站首页>关于元素位置和尺寸
关于元素位置和尺寸
2022-06-11 17:26:00 【大船叔叔】
元素位置的判断
浏览器,html元素 屏幕都有坐标系,原点是左上角 x轴往右 y轴往下
目标元素指的是 : 触发事件的元素
- 1.求点击的位置在目标元素上的元素
- (e.offsetX,e.offsetY)
- 2.求点击位置在浏览器上的坐标
- (e.clientX,e.clientY)
- 3.求点击位置到屏幕上的坐标 基本不用
- (e.screenX,e.screenY)
// 可以用来判断边框的大小
// 相对于元素自身边框的距离 如果没有边框 结果为0 也就是边框多大 他多大
div.clientLeft
div.clientTop
// 相对于非静态定位的父元素左上角的位置距离
//(1).如果父辈元素中有定位的元素,那么就返回距离当前元素最近的定位元素边缘的距离。
//(2).如果父辈元素中没有定位元素,那么就返回相对于body左边缘距离。
// 注意:offsetLeft 和 offsetTop 只能取值 不能赋值
// 如果要修改元素的位置 使用定位方式
div.offsetLeft
div.offsetTop
// div3.offsetLeft = 100; 不可以通过赋值进行修改
// 使用定位个margin可以修改 border和padding不可以修改
div3.style.position = 'absolute';
div3.style.left = '100px';
console.log('div3:' + div3.offsetLeft);
console.log('div3:'+ div3.offsetTop);
元素尺寸
// 1.显示器屏幕的尺寸
console.log(screen.width);
console.log(screen.height);
// 2.网页的尺寸 当前网页的可视区域大小(可能随着页面大小而改变)
console.log(document.documentElement.clientHeight);
console.log(document.documentElement.clientWidth);
// 同上
console.log(window.innerHeight);
console.log(window.innerWidth);
// html标签的大小
console.log(document.documentElement.offsetHeight);
console.log(document.documentElement.offsetWidth);
// 标签尺寸
var div = document.getElementById('divID');
// 查看元素width height
console.log(div.style.width);
console.log(div.style.height);
// 修改元素宽高
div.style.height = '200px';
div.style.width = '200px';
// 无法获取到width和height
var div1 = document.getElementById('divID1');
// 查看元素width height
console.log(div1.style.width);
console.log(div1.style.height);
// 总结 div.style.width 和 div.style.height 只能获取行内样式和js设置的width和height属性
// 不能获得内部样式css中写的width和height
// 内尺寸 不包含边框 包含padding + height和width
div1.clientHeight
div1.clientWidth
// 外尺寸 包含边框和padding +height和width
div1.offsetHeight
div1.offsetWidth
边栏推荐
- Remove key lookup bookmark from SQL Server
- Test basis: black box test
- Typescipt Basics
- Vscode configures eslint to automatically format an error "auto fix is enabled by default. use the single string form“
- Leetcode-- array
- Port planning and APJ
- mysql 大表的拆分方式
- 6-5 统计单词数量(文件)(*)
- Mathematical basis of information security Chapter 1 - Division
- Semaphore PV operation of process interaction and its code implementation
猜你喜欢

Authing 双周动态:Authing 论坛上线(4.25-5.8)

你还不懂线程池的设计及原理吗?掰开揉碎了教你设计线程池

搜狐全员遭诈骗,暴露哪些问题?

Derivation of child numbering formula for nodes numbered I in full k-ary tree

Authing biweekly news: online application market (5.10-5.22)

Automated testing selenium

Axi protocol Basics

Don't you understand the design and principle of thread pool? Break it up and crush it. I'll teach you how to design the thread pool

Vscode configures eslint to automatically format with an error "the setting is deprecated. use editor.codeactionsonsave instead with a source“

Vscode configures eslint to automatically format an error "auto fix is enabled by default. use the single string form“
随机推荐
6-2 写文章(*)
tidb-写热点的测试及分析
Analyze which should be tested in PMP and ACP with actual cases? Which is more useful?
C language: use H and C. summary of problems encountered in documents
Authing 双周动态:应用市场上线(5 .10 —5 .22 )
[MySQL] detailed explanation of redo log, undo log and binlog (4)
小白在同花顺上直接办理账户是安全的吗?
CentOS7服务器配置(四)---安装redis
字符串转数值
Biden ordered to enforce the zero trust structure
MFSR:一种新的推荐系统多级模糊相似度量
Go path: goroot & gopath
6-7 文件读写操作
Mathematical basis of information security Chapter 4 -- quadratic residual and square root
Vscode configures eslint to automatically format an error "auto fix is enabled by default. use the single string form“
ForEach遍历集合、 集合容器
聚类方法汇总
子类继承了什么、多态、 向上转型
搜狐全员遭诈骗,暴露哪些问题?
From manufacturing to "intelligent manufacturing", explore the way for manufacturing enterprises to break the situation