Animation CSS3: display + opacity

前端 未结 15 2956
轮回少年
轮回少年 2020-11-27 11:42

I have got a problem with a CSS3 animation.

.child {
    opacity: 0;
    display: none;

    -webkit-transition: opacity 0.5s ease-in-out;
    -moz-transit         


        
15条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 12:08

    If you are triggering the change with JS, let's say on click, there is a nice workaround.

    You see the problem happens because the animation is ignored on display:none element but browser applies all the changes at once and the element is never display:block while not animated at the same time.

    The trick is to ask the browser to render the frame after changing the visibility but before triggering the animation.

    Here is a JQuery example:

        $('.child').css({"display":"block"});
        //now ask the browser what is the value of the display property
        $('.child').css("display"); //this will trigger the browser to apply the change. this costs one frame render
        //now a change to opacity will trigger the animation
        $('.child').css("opacity":100);
    

提交回复
热议问题