css transitions on new elements

后端 未结 3 2094
长情又很酷
长情又很酷 2020-12-01 05:03

I cannot find a way to use css transitions on newly created dom elements.

let\'s say I have an empty html document.



        
相关标签:
3条回答
  • 2020-12-01 05:42

    In Firefox, it does appear to be a race between layout completing and the CSS transition. Chrome is much more predictable. If I set the class name on a setTimeout(), Chrome always works, Firefox only works if the setTimeout() time is long.

    With this code in Firefox (even using the setTimeout()), the text shows immediately:

    function f() {
        var a = document.createElement('a');
        a.id = 'id';
        a.innerHTML = ' fading in?';
        document.getElementsByTagName('p')[0].appendChild(a);
        // at this point I expect the span element to be with opacity=0
    
        setTimeout(function() {
            a.className = 'fadeIn';
        }, 10);
        return false;
    }
    

    But, if I force a reflow by requesting a property that can only be returned after layout, it then starts to work in Firefox:

    function f() {
        var a = document.createElement('a');
        a.id = 'id';
        a.innerHTML = ' fading in?';
        document.getElementsByTagName('p')[0].appendChild(a);
        // at this point I expect the span element to be with opacity=0
    
        // request property that requires layout to force a layout
        var x = a.clientHeight;
        setTimeout(function() {
            a.className = 'fadeIn';
        }, 10);
        return false;
    }
    

    Furthermore, once I've request that property to force a layout, I can even remove the setTimeout() and the animation works in Firefox.

    function f() {
        var a = document.createElement('a');
        a.id = 'id';
        a.innerHTML = ' fading in?';
        document.getElementsByTagName('p')[0].appendChild(a);
        // at this point I expect the span element to be with opacity=0
    
        // request property that requires layout to force a layout
        var x = a.clientHeight;
        a.className = 'fadeIn';
        return false;
    }
    

    You can see this last one work here in both Chrome and Firefox: http://jsfiddle.net/jfriend00/phTdt/

    And, here's an article that discusses the phenomenon: http://gent.ilcore.com/2011/03/how-not-to-trigger-layout-in-webkit.html

    0 讨论(0)
  • 2020-12-01 05:49

    I found a nicer way to trigger layout and make transitions work just after appending the element to the DOM:

    window.getComputedStyle(element).opacity;
    
    0 讨论(0)
  • 2020-12-01 05:59

    requestAnimationFrame() (https://developer.mozilla.org/en-US/docs/Web/API/window.requestAnimationFrame) appears to work across Firefox, Chrome and Safari. A more reliable, logical solution that setTimeout(). For older browsers (IE8), it will require a Polyfill (naturally, the transition won't occur, but the CSS will still change).

    0 讨论(0)
提交回复
热议问题