【发布时间】:2021-07-05 09:07:01
【问题描述】:
我的目标是创建一个 div,然后将其转换到正确的位置。
// the HTML comes from a source, can't change this
var TheHTML = "<div id=\"abc\"></div>";
// HTML to Element
var TempDiv = document.createElement('div');
TempDiv.innerHTML = TheHTML;
var TheNewElement = TempDiv.firstChild;
// adding the new element
document.getElementById("container").appendChild(TheNewElement);
// adding transitions and style
document.getElementById("abc").style.transition = "left 1s, width 1s";
document.getElementById("abc").style.left = "100px";
document.getElementById("abc").style.width = "100px";
这不起作用,它只是在没有任何动画过渡的情况下生成。但是,如果我在附加 div 后添加一个小延迟,它会起作用。 示例:
// the HTML comes from a source, can't change this
var TheHTML = "<div id=\"abc\"></div>";
// HTML to Element
var TempDiv = document.createElement('div');
TempDiv.innerHTML = TheHTML;
var TheNewElement = TempDiv.firstChild;
// adding the new element
document.getElementById("container").appendChild(TheNewElement);
// break for short pause
alert("break");
// adding transitions and style
document.getElementById("abc").style.transition = "left 1s, width 1s";
document.getElementById("abc").style.left = "100px";
document.getElementById("abc").style.width = "100px";
我更喜欢不需要等待某个计时器的解决方案,但如果这是一个选项而不是某个硬编码的计时器,则可以等待附加完成。
【问题讨论】:
标签: javascript css css-transitions