JS:获取元素宽高的几种方式

news/2024/7/19 14:09:30 标签: js

简介

JS获取元素宽高时,有的获取不到想要的值。
借鉴原文:链接地址 >

具体实现

// HTML
<div id="documentLabel " style="height: 100px;"></div>

// CSS
#documentLabel {
  background-color: aquamarine;
  width: 100px;
}

1.el.style.width/height
注意:style对象只能获取内联样式(dom中的style里的样式)

const el= document.getElementById('documentLabel')
console.log(el.style.width) // "" style对象取不到 style标签中定义的样式
console.log(el.style.height) // "100px"

2.window.getComputedStyle(element).width/height
window.getComputedStyle()可以实时获得style属性
MDN参考资料

const el = document.getElementById('documentLabel')
console.log(window.getComputedStyle(el).width)
console.log(window.getComputedStyle(el).height)

3.Element.getBoundingClientRect().width/height
MDN参考资料

const el = document.getElementById('documentLabel')
console.log(el.getBoundingClientRect().width)
console.log(el.getBoundingClientRect().height)

4.元素上的其他属性字段

const el = document.getElementById('documentLabel')
console.log(el.clientWidth) // 可见区域宽
console.log(el.clientHeight) // 可见区域高
console.log(el.offsetWidth) // 可见区域宽 + 边线的宽
console.log(el.offsetHeight) // 可见区域高 + 边线的宽
console.log(el.scrollWidth) // 正文全文宽
console.log(el.scrollHeight) // 正文全文高
console.log(el.screenTop) // 被卷去的高
console.log(el.scrollLeft) // 被卷去的左

拓展

关于浏览器的几个属性

console.log(window.screenTop) // 网页正文部分上
console.log(window.screenLeft) // 网页正文部分左
console.log(window.screen.height) // 屏幕分辨率的高
console.log(window.screen.width) // 屏幕分辨率的宽
console.log(window.screen.availHeight) // 屏幕可用工作区高度
console.log(window.screen.availWidth) // 屏幕可用工作区宽度

最后

觉得有用的朋友请用你的金手指点一下赞,或者评论留言一起探讨技术!


http://www.niftyadmin.cn/n/1867225.html

相关文章

170715 personal links for reference

CSDN去水印 300个数据集 svm中one-versus-rest和one-versus-one的不同 Anaconda使用总结 python packages 面试70问&#xff0c;经典回答

JS:去除字符串左端空格、字符串右端空格、字符串两端空格

简介 去除字符串左端空格、字符串右端空格、字符串两端空格的简易方法。 具体实现 //去左空格; function ltrim(s){return s.replace(/(^\s*)/g, ""); } //去右空格; function rtrim(s){return s.replace(/(\s*$)/g, ""); } //去左右空格; function tri…

170715 Keras Learning Notes(TBC)

tensorflow 与 theano中关于图像维度的定义不同 tensorflow: (64,64,3) # rgb在末端 theano: (3,64,64) # rgb在前端 from keas import backend as K K.set_image_dim_ordering(tf) Getting Tensorflow, Theano and Keras on Windows

VUE:使用element-ui的el-table时,自定义单元格内容,并tab快速切换指定编辑的单元格,而不是把所有能tab切换的都切换一遍

简介 操作 element-ui 的 el-table 时&#xff0c;想要快速tab切换光标到指定的列的输入框中&#xff0c;而不是把一行有聚焦的都tab切换选中一遍&#xff08;如有el-button时&#xff0c;按tab切换也会切换到它上面去&#xff09;。 并且鼠标单击输入框时&#xff0c;自动全选…

170716 from numpy._distributor_init import NUMPY_MKL # requires numpy+mkl

stackover answer Whl下载安装地址 command window # uninstall old pip uninstall scipy pip uninstall numpy # install new pip install numpy_absolute_path pip install scipy_absolute_path

VUE:echarts如何适应宽度【附封装的简单柱状图组件页面参数设置】

简介 在使用echarts时&#xff0c;发现它不是自适应的。 比如浏览器全屏时打开了有echarts的页面&#xff0c;加载完后&#xff0c;缩小窗口&#xff0c;就会很奇怪&#xff0c;所以研究了一下怎么实现主动适应。 具体实现 在app.vue中通过window.onresize事件进行实时存储…

170716 网线接口顺序

第一次接网线&#xff0c;以下是接口的顺序&#xff0c;Mark一下

VUE:axios的post、delete请求,只传一个数组时,后台接收不到数据

简介 axios的delete、post请求&#xff0c;只传一个数组时&#xff0c;后台接收不到数据&#xff0c;接口就报错400。 查看了请求接口的请求头中的信息&#xff0c;发现&#xff0c;“Content-Type"是"text/plain” 具体实现 解决的方式很简单&#xff0c;在axi…