简体   繁体   English

在不知道最终大小的情况下使用jQuery对DIV大小进行动画处理

[英]Animate DIV size using jQuery without knowing the final size

I have a DIV with content that is inserted dynamically using jQuery. 我有一个DIV,其内容是使用jQuery动态插入的。 I want that div to appear as if it was enlarged from size 0px to the size it suppose to take when the div is rendered with its content. 我希望该div看起来好像从大小0px放大到其内容渲染div时假定的大小。

The content is text, so I have no information about the size it will take when it is appended to the document. 内容是文本,因此我没有关于将其附加到文档时将要使用的大小的信息。 I know how to animate the div using jQuery Animate when I know the final size, but don't know how to do it when I don't know it. 我知道最终大小时如何使用jQuery Animate为div设置动画,但是当我不知道最终大小时不知道如何做。

if I use width: "+=10" for example, I get a div that is larger than the one that I get without animating. 例如,如果我使用width: "+=10" ,则得到的div大于没有动画设置的div。 Thanks. 谢谢。

Insert your item, save the width and the height, and then set the width and the height of the item to zero. 插入您的项目,保存宽度和高度,然后将项目的宽度和高度设置为零。 This will happen too quickly to be visible, and you'll have the final width and height values you need for animation. 这将发生得太快而无法看到,并且您将获得动画所需的最终宽度和高度值。

Live Demo 现场演示

var $div = $('<div>', {
    text: <your text>
});

// <append your element>

var width = $div.width();
var height = $div.height();

$div.css({
    width: 0,
    height: 0
});

$div.animate({
    width: width,
    height: height
}, {
    duration: 'slow',
    easing: 'linear'
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM