测量元素尺寸时忽略过渡

jan*_*mon 5 html javascript css jquery

在元素转换完成后,是否有一种优雅的方法来预测元素的尺寸

例:

HTML:

<div id="demo">...</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

#demo {
  max-height: 0;
  overflow:hidden;
  transition: 2s max-height;
}

#demo.expand {
  max-height: 1000px;
}
Run Code Online (Sandbox Code Playgroud)

JS:

 var demo = document.getElementById('demo');
 demo.className = 'expand';
 // Unfortunately the result will be 0px 
 // because the current height is measured
 alert(demo.offsetHeight);
Run Code Online (Sandbox Code Playgroud)

演示:

codepen