Skip to content

Instantly share code, notes, and snippets.

@337547038
Last active March 28, 2018 14:45
Show Gist options
  • Save 337547038/a108712c2ebcd53d09c83c6d3588d6db to your computer and use it in GitHub Desktop.
Save 337547038/a108712c2ebcd53d09c83c6d3588d6db to your computer and use it in GitHub Desktop.
获取隐藏元素的宽高
/**
* 获取隐藏元素的宽高
* @param {Object} obj
*/
function getDomWidthOrHeight(widthOrHeight,obj){
//console.log(widthOrHeight+"="+obj);
var clone=obj.cloneNode(true);
clone.style.display="block";
clone.style.position="absolute";
  clone.style.top=-10000px;
obj.parentNode.appendChild(clone);
var width=clone.offsetWidth;
var height=clone.offsetHeight;
//console.log(width+"--"+height);
obj.parentNode.removeChild(clone);
return widthOrHeight=="width"?width:height;
}
/*另外一种更直接简单的方法*/
function getHeight(obj){
obj.style.visibility='hidden';
//obj.style.opacity=0;
obj.style.display="block";
obj.style.position="absolute";
  obj.style.top=-10000px;
var height=obj.offsetHeight;
obj.style=''
return height;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment