[英]CSS animation restarted on HTML injection
I'm applying CSS animations to the content of a page. 我正在将CSS动画应用于页面的内容。 The animations are loaded dynamically and there I have a behavior I don't understand : I play the first animation and it works fine.
动画是动态加载的,并且有一种我不了解的行为:我播放了第一个动画,并且效果很好。 Then I add and play the second animation and then the first animation is played again, together with the second animation.
然后,我添加并播放第二个动画,然后与第二个动画一起再次播放第一个动画。 How can I get rid of this ?
我该如何摆脱呢?
var container = document.getElementById("container"); function addFadeOut() { container.innerHTML += "<div id=\\"be\\" style=\\"position:absolute;z-index:2;opacity:0;width:100%;height:100%;background-color: green;animation: fadeOut 3s forwards;\\" />" } function addFadeIn() { container.innerHTML += "<div id=\\"bb\\" style=\\"position:absolute;z-index:1;opacity:0;width:100%;height:100%;background-color: red;animation: fadeIn 3s forwards;\\" />" } setTimeout(addFadeOut, 4000); addFadeIn();
@keyframes fadeIn { 0% { opacity: 1; } 100% { opacity: 0; } } @keyframes fadeOut { 0% { opacity: 0; } 100% { opacity: 1; } } #container { position: absolute; width: 300px; height: 200px; background-color: cyan; } .content { position: absolute; width: 100%; height: 100% }
<div id="container"> <div class="content"/> </div>
CSS animations trigger on an element whenever it is inserted in the DOM. 每当将元素插入DOM时,CSS动画就会在该元素上触发。
The first item (with the fadeIn
animation) is re-created when you use innerHTML
again 4 seconds later (which means that the previous DOM element representing that div is removed, and another identical element is inserted). 4秒钟后再次使用
innerHTML
时,将重新创建第一项(带有fadeIn
动画)(这意味着删除了表示div的前一个DOM元素,并插入了另一个相同的元素)。
If, as Thomas says, you insert the elements with explicit DOM manipulation, you can avoid that: 如Thomas所述,如果您通过显式DOM操作插入元素,则可以避免这种情况:
var container = document.getElementById("container");
function addFadeOut() {
var el = document.createElement("div")
el.style="position:absolute;z-index:2;opacity:0;width:100%;height:100%;background-color: green;animation: fadeOut 3s forwards;"
container.appendChild(el)
}
function addFadeIn() {
var el = document.createElement("div")
el.style="position:absolute;z-index:1;opacity:0;width:100%;height:100%;background-color: red;animation: fadeIn 3s forwards;"
container.appendChild(el)
}
setTimeout(addFadeOut, 4000);
addFadeIn();
Don't use innerHTML +=
, use appendChild()
with actual DOM-Nodes. 不要使用
innerHTML +=
,而是将appendChild()
与实际的DOM节点一起使用。 Maybe like the following: 可能如下所示:
//a simple Utility to convert html into a documentFragment (can contain multiple DOM-nodes)
var dummy = document.createElement('div');
function fromHTML(html){
var node, frag = document.createDocumentFragment();
for(dummy.innerHTML = html; node = dummy.firstChild; frag.appendChild(node));
return frag;
}
and your adapted code 和您改编的代码
var container = document.getElementById("container");
function addFadeOut() {
var markup = "<div id=\"be\" style=\"position:absolute;z-index:2;opacity:0;width:100%;height:100%;background-color: green;animation: fadeOut 3s forwards;\" />";
container.appendChild(fromHTML(markup));
}
function addFadeIn() {
var markup = "<div id=\"bb\" style=\"position:absolute;z-index:1;opacity:0;width:100%;height:100%;background-color: red;animation: fadeIn 3s forwards;\" />";
container.appendChild(fromHTML(markup));
}
setTimeout(addFadeOut, 4000);
addFadeIn();
the reason for this reset is that 重置的原因是
container.innerHTML += markup;
is the same as 是相同的
container.innerHTML = container.innerHTML + markup;
wich is also the same as wich也与
var newMarkup = container.innerHTML + markup;
container.innerHTML = newMarkup;
and now you should see, that by assigning a new value to innerHTML
the node first throws every current childNode away, and created new ones from the passed newMarkup
. 现在您应该看到,通过为
innerHTML
分配一个新值,该节点首先将当前的每个childNode丢弃,并从传递的newMarkup
创建新的newMarkup
。 To you, the former and the new node look the same, to JS they are not. 对您而言,前者和新节点看起来相同,而对于JS而言,它们则不同。 they may be equal, but not identical.
它们可以相等,但不相同。 Therefore: new node, new animation.
因此:新节点,新动画。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.