对我尝试使用 GetBoundingClientRect() 感到困惑

Confused by my attempt to use GetBoundingClientRect()

本文关键字:GetBoundingClientRect      更新时间:2023-09-26
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/

您已经记录了一个静态字符串:

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

你的意思是写这个:

console.log(rect.top, rect.right, rect.bottom, rect.left);
相关文章:
  • 没有找到相关文章