简体   繁体   English

CSS3-添加动态内容会触发动画

[英]CSS3 - Adding dynamic content triggers animation

I have animation defined in CSS3 for specific class which runs only one time when that class is added to a element. 我在CSS3中为特定类定义了动画,当该类添加到元素中时该动画只能运行一次。 The problem is when I add dynamic content to their parent the animation runs for one more time. 问题是,当我向其父级添加动态内容时,动画又运行了一次。 Everytime, content is added the animation triggers. 每次都添加动画触发器的内容。 A list element which is to be animated is automatically added to ul and it animates one time (expected behaviour) whereas, the non-animable elements are added on JS Event. 将要动画化的list元素会自动添加到ul并且会动画一次(预期行为),而在JS Event上会添加非动画元素。

HTML: HTML:

<ul>
    <li class="m"><div class="animable"></div></li>
    <li class="m"><div class="animable animateNow"></div></li>
</ul>

When JS event occurs, a list element is added dynamically. 发生JS事件时,将动态添加一个列表元素。

<ul>
    <li class="m"><div class="animable"></div></li>
    <li class="m"><div class="animable animateNow"></div></li>
    <li class="m"><div class="animable"></div></li> // <- Sir! I am innocent. I am newly added by JS. IDK, what is happening :/
</ul>

and the moment it is appended , the animable div triggers its animation for one more time and so on for each added element. 追加后,animable div会再触发一次动画,依此类推。

Is this a bug ? 这是一个错误吗? How can I prevent it? 我该如何预防? I am using pure JS. 我正在使用纯JS。

CSS: CSS:

.animable {
    position:absolute;

    top:0;
    left:0;
    right:0;
    bottom:0;

    width:100%;
    height:100%;

    background:#ecf7d2;
}
.animateNow {
    opacity:0;
    animation-duration: 2s;
    animation-name: animTrans;
    -webkit-animation-duration: 2s;
    -webkit-animation-name: animTrans;
}

大形象

您可以使用我认为的preventdefault函数,但看不到js部分,也不能摆弄您很难建议共享JS部分的内容

On our discussion in the chat, you showed the code that adds the items: 在聊天中的讨论中,您显示了添加项目的代码:

var elem = document.getElementById("messages").getElementsByTagName("ul")[0];
elem.innerHTML += msg.data;

Your problem is that you're changing the innerHTML every time, which makes the entire content of the list to render again and again... 您的问题是您每次都要更改innerHTML,这会使列表的整个内容一次又一次地呈现。

You should create a DOM element and use Node.appendChild( child ) instead. 您应该创建一个DOM元素,并改用Node.appendChild( child

var elem = document.getElementById("messages").getElementsByTagName("ul")[0];

var item = document.createElement('LI');
item.outerHTML = msg.data;

elem.appendChild(item)

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

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