获取元素位置-{绝对|相对|固定}返回空值?

Viv*_*dra 3 javascript position

使用javascript-我们可以设置元素的相对位置,例如

object.style.position="absolute"||"fixed"||"relative"

但是,在使用相同console.log(object.style.position)对象时-它不会返回应用于对象的位置-会返回NULL。我在这里错过了什么吗,还是有另一种方式来实现我想要达到的目标?

pim*_*vdb 5

.style表示元素本身设置的内容,与style属性非常相似。

您可以改用getComputedStylehttp : //jsfiddle.net/qAbTz/1/

var div = document.getElementById("div");

console.log(div.style.position);              // "" (not null by the way)
console.log(getComputedStyle(div).position);?  // "fixed"
Run Code Online (Sandbox Code Playgroud)