https://segmentfault.com/a/1190000015961743
看了以上博客后的自我总结:
offset是做什么用的呢?
事实上是为了获取元素的长和宽、以及位置,是一个只读属性,不可更改。
别说获取元素的长宽可以使用objec.style.width/height,这个只能获取写在行内样式中的元素长宽,否则返回为空。
如下:
写在行内样式中
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="box" style="width:100px;height:100px;background: pink">hahahah</div>
</body>
<script>
var box = document.getElementById("box");
console.log(box.style.width);
</script>
</html>
结果: