这个答案的灵感来自 Adeneo 的答案,但是是在已经存在的元素而不是副本上完成的。在许多情况下,很难使副本具有与原始元素相同的内容和样式,因此处理原始元素可能更容易。
这是我修改您的代码示例的方式:
<style>
.brick
{
}
.transition
{
transition: all 0.4s ease-in-out;
}
.large
{
width: 400px;
height: 400px;
}
.small
{
width: 200px;
height: 200px;
}
</style>
<div class="brick small"></div>
<script>
$('.brick').height(); // returns 200px
$('.brick').width(); // returns 200px
// Apply final styling and get dimensions.
$('.brick').removeClass('small').addClass('large');
$('.brick').height(); // returns 400px
$('.brick').width(); // returns 400px
// Restore styling to what it was before measuring dimensions.
// Note width() and/or height() must be called after restoring the
// initial style to force layout to update. If you don't, the
// transition won't work.
$('.brick').addClass('small').removeClass('large');
$('.brick').height(); // returns 200px.
$('.brick').width(); // returns 200px.
// Apply final styling again, but include transition.
$('.brick').removeClass('small').addClass('large transition');
</script>
在 JS 小提琴中:http:
//jsfiddle.net/qptwnq5p/