简体   繁体   English

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

[英]How to change an element's position to absolute?

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.我认为最好的方法是将一个表格相对定位在页面中心的开头,然后在页面onload时将其更改为绝对定位。 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 .但是,我在询问对象左侧在哪里之后放了一个警告框,我得到了undefined But, if I directly assign it to a position ( Object.style.left = 500 ) I will get that number.但是,如果我直接将它分配给一个位置( Object.style.left = 500 ),我会得到那个数字。 So I can assign it a value, meaning it is in absolute position, but I cannot pull a value out of that.所以我可以给它赋值,这意味着它处于绝对位置,但我不能从中提取一个值。

use document.getElementById("elementName").offsetLeft to find the position of an object使用document.getElementById("elementName").offsetLeft查找对象的位置
relative to its parent-container's position [in your case, document.body]相对于其父容器的位置 [在你的情况下,document.body]
You're accessing the CSS style object's left parameter, which is different.您正在访问 CSS 样式对象的左参数,这是不同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM