2
$('.container').animate({ 'height': el.height()  }, { duration: 300});

所以el的高度可以小于或大于.container的高度。

当当前高度与我正在制作动画的高度之间的差异较大时,如何才能使效果持续时间更长?

4

3 回答 3

3
var newHeight = el.height(),
    oldHeight = $('.container').height();
$('.container').animate({height: newHeight}, Math.abs(newHeight-oldHeight)*5);

将魔法常数更改5为任何看起来合理的值。您没有提供计算持续时间的标准;您可以将它与 绑定Math.min(/*above expression*/, maxDuration),或者它不应该是线性的,而是对数的。你可以很容易地自定义它。虽然这是一个很好的起点。

于 2011-06-18T17:35:04.397 回答
1
if ($(.container).height() - $(el).height() > 0)
{
    // Whatever you want to do if the container is higher than el
}
else
{
    // Other case
}

不确定它是否是正确的 JS 语法,但它应该可以工作。

http://api.jquery.com/height/

编辑:没关系,看看戴文的回答。

于 2011-06-18T17:25:59.033 回答
1
$('.container').animate({height:el.height()},($(this).height()-el.height()>el.height())?3000:100);
于 2011-06-18T17:39:27.467 回答