Interruption of a CSS transition does not work for same attribute value

后端 未结 2 1410
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-14 03:32

I\'ve answered a question on how to start an animation when hovering the child element and then preserve the applied style until un-hovering the parent. However, I discovered a

2条回答
  •  北恋
    北恋 (楼主)
    2021-02-14 03:42

    Firstly, I change the transition-delay of .parent .child:

    .parent .child{
        transition: width 0.5s ease 6s;
    }
    

    It works and nothing is wrong. But wait a minute!

    .parent:not(:hover) .child{
        transition: width .5s ease 0s;
        width: 10px;
    }
    

    Why doesn't the property work if the width property is 10px?

    In fact, the code above is equivalent to:

    .parent:not(:hover) .child{
        transition: width .5s ease 0s;
    }
    

    According to the spec provided by W3,

    when a transition is started for a property on an element (henceforth, the new transition) that has a currently-running transition whose reversing-adjusted start value is the same as the end value of the new transition (henceforth, the old transition), implementations must cancel the old transition link to definition above and adjust the new transition as follows (prior to following the rules for computing the combined duration, start time, and end time):

    So it seems like that the transition-delay property should be reset to 0s. However, I think it's something mentioned by W3C called style change event:

    Since this specification does not define when a style change event occurs, and thus what changes to computed values are considered simultaneous, authors should be aware that changing any of the transition properties a small amount of time after making a change that might transition can result in behavior that varies between implementations, since the changes might be considered simultaneous in some implementations but not others.

    Since the end value of the "new" transition we define in .parent:not(:hover) .child is the same as that in .parent .child, the so-called "new" transition is not regarded as a new transition by the browser. In this case, the transition-delay property won't be reset of course.

    However, if we change width of .parent:not(:hover) .child, that is, change the end value of the transition, we are thought to have defined a new transition and according to the spec mentioned above, the transition-delay property is reset to 0s.

提交回复
热议问题