Javascript操作CSS之3——获取元素的真实高度和宽度

 

元素的真实高度可以用getStyle(获取元素属性的方法)求的,但是还要考虑到元素的隐藏等原因,因此需要一个通用的办法。

function getFullHeight(elem)

{

if (getStyle(elem,display)!=="none")

{

return elem.offsetHeight || getStyle(elem,"height");

}

else

{

 

var old=resetCss(elem,{

display:"",

visibility:"hidden",

position,"absolute"

});

var fullheigh=elem.offsetHeigh||getStyle(elem,"heigh");

resoreStyles(elem,old); 

 

}

}

 

function getFullWidth(elem)

{

if (getStyle(elem,"display")!=="none")

{

return elem.offsetWidth||getStyle(elem,"width");

}

else

{

var old=resetCss(elem,{

display:"",

visibility:"hidden",

position,"absolute"

});

}

}

 funcion resetCss(elem,styles)

{

var old ={};

for (var i in styles)

{

old[i]=styles[i];

elem.style[i]=styles[i];

}

return old;

}

function resoreStyles(elem,old)

{

for(var i in old)

{

elem.style9i]=old[i];

}

}

 

你可能感兴趣的:(Javascript)