简体   繁体   English

使用 javascript 创建的新 div 的 CSS 转换

[英]CSS transition for new div created with javascript

My goal is to create a div and then transition it to it's correct position.我的目标是创建一个 div,然后将其转换为正确的 position。

// 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";

This does not work, it just spawns without any animated transition.这不起作用,它只是在没有任何动画过渡的情况下生成。 However, if I add a small delay after appending the div, it works.但是,如果我在附加 div 后添加一个小延迟,它就可以工作。 Example:例子:

// 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";

I would prefer a solution where I don't need to wait for some timer, but it would be okay to wait for the appending to finish if that's an option and not some hard-coded timer.我更喜欢不需要等待某个计时器的解决方案,但如果这是一个选项而不是一些硬编码的计时器,则可以等待附加完成。

I just found async functions and await .我刚刚找到 async 函数和await Solution:解决方案:

// 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;


// function so await can be used
async function MainFunc() {
    
    // adding the new element
    await 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";
    
};
MainFunc();

replace your更换你的

document.getElementById("abc").style.transition = "left 1s, width 1s";
document.getElementById("abc").style.left = "100px";
document.getElementById("abc").style.width = "100px";

with

document.getElementById('abc').animate([
    {left: '0', width: '0'},
    {left: '100px', width: '100px'}
], {
    duration: 1000,
    fill: 'forwards'
});

don't forget the fill: 'forwards' property.不要忘记fill: 'forwards'属性。 If you doesn't add this this animation will not persist如果您不添加此 animation 将不会持续存在

Why not combine js with css animations?为什么不将 js 与 css 动画结合起来呢?

 var TheHTML = "<div class=\"moveTransition\">Test</div>"; // HTML to Element var TempDiv = document.createElement('div'); TempDiv.innerHTML = TheHTML; // adding the new element document.querySelector(".container").appendChild(TempDiv);
 .moveTransition{ animation: move 1s ease forwards; } @keyframes move { to { transform: translateX(100px) } }
 <div class="container"> </div>

Before injecting the div include this CSS in the page在注入 div 之前,请在页面中包含此CSS

#abc {
  --left: 0;
  --width: 0;
  position: absolute; /* or relative/fixed */
  left: var(--left);
  width: var(--width);
  transition: left 1s, width 1s;
}

in particular if the div at the initial state is empty set width and left to 0 (or other meaningful value you like).特别是如果初始 state 的 div 为空集widthleft0 (或您喜欢的其他有意义的值)。 Also, the transition can be already included in the style itself, since the value of this property appears to be statically defined, not dependant on the Javascript.此外,过渡可能已经包含在样式本身中,因为此属性的值似乎是静态定义的,不依赖于 Javascript。

Then, when you append a div just change the variables然后,当你 append 一个div只需更改变量

let abc = document.getElementById("abc");
abc.style.setProperty('--width', '100px');
abc.style.setProperty('--left',  '100px');

so the animation will be triggered, changing from a default value (not auto ) to the new one, and a delay won't be necessary.因此 animation 将被触发,从默认值(不是auto )更改为新值,并且不需要延迟。

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

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