简体   繁体   English

在添加新的DOM子元素的同时显示动画

[英]Showing animation while adding a new DOM sub element

I have a list of divs in parent div. 我在父div中有一个div列表。 I'm now adding add another div in this list at the beginning. 我现在在开始时在此列表中添加另一个div。 I'm doing this: 我正在这样做:

$('.list').prepend($('<div class="cell one "></div>'));

My question is how do I animate this adding new div so that rest of the divs just slide down. 我的问题是如何为添加的div设置动画,以便其余div向下滑动。

Here is the JSFiddle for this: http://jsfiddle.net/2qbyB/ 这是为此的JSFiddle: http : //jsfiddle.net/2qbyB/

<div class="list">
   <div class="cell two "></div>
   <div class="cell three "></div>
   <div class="cell four "></div>
</div>

You cannot add animation to the List for the desired effect, instead you need on the new element. 您不能将动画添加到列表中以获得所需的效果,而是需要在新元素上。

Here is a simple example of sliding down (height animation) using JQuery .animate() function 这是一个使用JQuery .animate()函数向下滑动(高度动画)的简单示例

$('.list').prepend($('<div class="cell one "></div>').animate({height: "50px" }, 
1500 ));

For more detail on .animate() see http://api.jquery.com/animate/ . 有关.animate()的更多详细信息,请参见http://api.jquery.com/animate/

In case you do not want to use jQuery but native CSS animations, the animation won't be triggered when the class of the new element doesn't change. 如果您不想使用jQuery而是使用本机CSS动画,则当新元素的类不变时,不会触发该动画。 What you can do is to insert the element and then add the class after a timeout: 您可以做的是插入元素,然后在超时后添加类:

setTimeout(function() {
    $('.new').addClass('animate');
});

"new" and "animate" are of course custom classes for which you have to provide proper CSS. “ new”和“ animate”当然是自定义类,您必须为其提供适当的CSS。

You primarily want to use CSS animations for performance reasons. 出于性能原因,您主要想使用CSS动画。 See here: what's faster? 看到这里: 什么更快? CSS3 transitions or jQuery animations? CSS3过渡还是jQuery动画?

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

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