获取元素CSS大小
通过style内联获取元素的大小
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="box" style="width:100px;"></div>
</body>
<script type="text/javascript">
var box = document.getElementById('box');
console.log(box.style.width);//200px
console.log(box.style.height);//??
</script>
</html>
通过计算获取元素的大小
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
#box{
height:200px;
}
</style>
</head>
<body>
<div id="box" style="width:100px;"></div>
</body>
<script type="text/javascript">
var box = document.getElementById("box");
var style = window.getComputedStyle ?
window.getComputedStyle(box, null) : null || box.currentStyle;
console.log(style.width);//100px
console.log(style.height);//200px
</script>
</html>
通过css规则获取元素大小
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
#box{
height:200px;
}
</style>
</head>
<body>
<div id="box" style="width:100px;"></div>
</body>
<script type="text/javascript">
var sheet = document.styleSheets[0];//获取link或style
var rule = (sheet.cssRules || sheet.rules)[0];//获取第一条规则
console.log(rule.style.width);//空
console.log(rule.style.height);//200px
</script>
</html>
总结:以上的三种CSS获取元素大小的方法,只能获取元素的CSS大小,却无法获取元素本身实际的大小。比如加上了内边距、滚动条、边框之类的。
获取元素实际大小
clientWidth和clientHeight
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="box" style="width:100px;padding:10px;border:5px"></div>
</body>
<script type="text/javascript">
var box = document.getElementById("box");
console.log(box.clientWidth);//120
console.log(box.clientHeight);//20
</script>
</html>
scrollWidth和scrollHeight
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="box" style="border:5px solid red;box-sizing:border-box;width:100px;overflow-y: auto">
test test test test test test test test test
</div>
</body>
<script type="text/javascript">
var box = document.getElementById("box");
console.log(box.clientWidth);//90
console.log(box.clientHeight);//90
</script>
</html>
如果没有设置任何CSS的宽和高度,它会得到计算后的宽度和高度。offsetWidth和offsetHeight
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="box" style="width:100px;padding:10px;border:5px solid red;">aa</div>
</body>
<script type="text/javascript">
var box = document.getElementById("box");
console.log(box.offsetWidth);//130
console.log(box.offsetHeight);//48
</script>
</html>
获取元素周边大小
clientLeft和clientTop
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body style="margin:0">
<div id="box" style="width:100px;padding:10px;border:5px solid red;border-top-width:10px">aa</div>
</body>
<script type="text/javascript">
var box = document.getElementById("box");
console.log(box.clientLeft);//5
console.log(box.clientTop);//10
</script>
</html>
offsetLeft和offsetTop
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body style="margin:0;padding: 8px 16px;position: relative;">
<div id="box" style="width:100px;padding:10px;border:5px solid red;border-top-width:10px">aa</div>
<div id="box1" style="position: absolute;left:40px;top: 30px"></div>
</body>
<script type="text/javascript">
var box = document.getElementById("box");
console.log(box.offsetLeft);//16
console.log(box.offsetTop);//8
var box1 = document.getElementById("box1");
console.log(box1.offsetLeft);//40
console.log(box1.offsetTop);//30
</script>
</html>
获取元素当前相对于父元素的位置,最好将它设置为定位position:absolute;否则不同的浏览器会有不同的解释scrollTop和scrollLeft
元素位置
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body style="margin:0;padding: 8px 16px;position: relative;">
<div id="box" style="width:100px;padding:10px;border:5px solid red;border-top-width:10px">aa</div>
<div id="box1" style="position: absolute;left:40px;top: 30px;width:50px"></div>
</body>
<script type="text/javascript">
var box = document.getElementById('box');//获取元素
console.log(box.getBoundingClientRect().top);//8,元素上边距离页面上边的距离
console.log(box.getBoundingClientRect().right);//146,元素右边距离页面左边的距离
console.log(box.getBoundingClientRect().bottom);//61,元素下边距离页面上边的距离
console.log(box.getBoundingClientRect().left);//16,元素左边距离页面左边的距离
var box1 = document.getElementById('box1');
console.log(box1.getBoundingClientRect().top);//30
console.log(box1.getBoundingClientRect().right);//90
console.log(box1.getBoundingClientRect().bottom);//30
console.log(box1.getBoundingClientRect().left);//40
</script>
</html>
注意:IE、Firefox3+、Opera9.5、Chrome、Safari支持,在IE中,默认座标从(2,2)开始计算,导致最终距离比其他浏览器多出两个像素