A
A
artr_lr2017-08-18 18:48:19
JavaScript
artr_lr, 2017-08-18 18:48:19

Block animation immediately after adding it?

Is it possible to animate an element? Logically, the element receives dynamic styles and is immediately added to the page, where the expand and move animation should be triggered. But... this does not happen, and the block just gets the dynamic style of the final state without animation... But if you look through the chrome debugger, then everything works as it should!

.el {
    transition: transform .33s ease;
    background: red;
    width: 100px;
    height: 100px;
}

var div = document.createElement('div');
div.className = 'el';
div.style.transform = 'scale(.5) translate3d(100px, 100px, 0)'; // первоначальное состояние
document.body.appendChild(div);
div.style.transform = 'scale(1) translate3d(0px, 0px, 0)'; // конечное

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2017-08-18
@artr_lr

Wrap the last line in setTimeout(..., 0)
https://jsfiddle.net/6vecsx0h/

E
expeerd, 2017-08-18
@expeerd

I'm under the strong impression that CSS animations are a much more elegant and correct solution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question