>[danger]介绍一下Js中的getComputedStyle `getComputedStyle()`是一个可以获取当前元素所有最终使用的CSS属性值的方法。返回的是一个CSS样式声明对象。 这个方法有两个参数,第一个参数是你想要获取哪个元素的 CSS ,第二个参数是一个伪元素。 >[info]用法 ~~~ <style> #box { width: 200px; height: 200px; background-color: cornflowerblue; position: relative; } #box::after { content: ""; width: 50px; height: 50px; background: #000; position: absolute; top: 0; left: 0; } </style> ~~~ ~~~ const box = document.getElementById('box') const style = window.getComputedStyle(box, 'after') const height = style.getPropertyValue('height') const width = style.getPropertyValue('width') console.log(style); console.log(width, height); ~~~ 上述代码输出结果为: ![](https://img.kancloud.cn/9d/d5/9dd5654eb78f1f77f97455870dc1b9ab_232x46.png) 有一个 id 为 box 容器的 CSS 样式声明对象,以及伪元素的宽高。