我试图使用GetBoundingClientRect()感到困惑

时间:2013-09-16 02:49:19

标签: javascript

var rect = document.getElementById("object").getBoundingClientRect();
console.log("rect.top, rect.right, rect.bottom, rect.left");

<div id="object"></div>

.object {
background: red;
width: 20px;
height: 20px;
position: relative;
top: 200px;
}

超级基本问题...我试图获取#object的坐标,但控制台返回rect.top,rect.right,rect.bottom,rect.left而不是#object的实际坐标。我错过了什么?

JSFiddle:http://jsfiddle.net/tCraA/2/

1 个答案:

答案 0 :(得分:1)

您已记录静态字符串:

console.log("rect.top, rect.right, rect.bottom, rect.left");

你打算写这个:

console.log(rect.top, rect.right, rect.bottom, rect.left);