如何将元素的位置更改为绝对位置?

发布于 2024-08-29 03:22:19 字数 354 浏览 3 评论 0原文

我试图将页面置于任何分辨率的中心,同时仍然能够使用绝对定位来移动图片。我认为最好的方法是将表格相对定位在页面中心的开头,然后在页面的 onload 中将其更改为绝对定位。我认为正确的行是:

document.getElementById("Object").style.position = "absolute";

但是,我在询问对象左侧在哪里之后放置了一个警告框,我得到未定义。但是,如果我直接将其分配给一个位置 (Object.style.left = 500),我将得到该数字。所以我可以给它赋一个值,这意味着它处于绝对位置,但我无法从中提取值。

I'm trying to center a page on any resolution while still being able to use absolute positioning to move pictures. I figure that the best way would be to have a table positioned relatively in the beginning in the center of the page, then change it to absolute positioning in the onload of the page. I think the correct line is:

document.getElementById("Object").style.position = "absolute";

However, I put an alert box after it asking where the left side of the object is, I get undefined. But, if I directly assign it to a position (Object.style.left = 500) I will get that number. So I can assign it a value, meaning it is in absolute position, but I cannot pull a value out of that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

护你周全 2024-09-05 03:22:19

使用 document.getElementById("elementName").offsetLeft 查找对象的位置
相对于其父容器的位置[在您的情况下,document.body]
您正在访问 CSS 样式对象的 left 参数,该参数是不同的。

use document.getElementById("elementName").offsetLeft to find the position of an object
relative to its parent-container's position [in your case, document.body]
You're accessing the CSS style object's left parameter, which is different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文