CSS transition from `display: none` on class change?

后端 未结 4 1114
一生所求
一生所求 2020-12-14 14:41

I\'ve started using transitions to \"modernise\" the feel of a site. So far, :hover transitions are working great. Now I\'m wondering if it\'s possible to trigg

4条回答
  •  孤街浪徒
    2020-12-14 15:24

    The answer provided by @MarcoK including the comments shows already the right direction. Setting display property hinders transition.
    A better practice is though to put the unprefixed (standards) version after the browser-vendor prefixed ones, in order to be future-proof. The latter properties overwrite the former.
    Other improvements:

    • As @Charmander pointed out, -ms-transition isn't supported by any Internet Explorer
    • There's also Opera's vendor prefixed -o-transition for Op 10.5-12 & Op Mobile 10-12, which currently is probably supported by less than .1% of global browser. I'll put it in for completion

    CSS:

    #myelem {
        opacity: 0;
        -webkit-transition: opacity .4s ease-in;
           -moz-transition: opacity .4s ease-in;
             -o-transition: opacity .4s ease-in;
                transition: opacity .4s ease-in;
    }
    #myelem.show {
        opacity: 1;
        -webkit-transition: opacity .4s ease-out;
           -moz-transition: opacity .4s ease-out;
             -o-transition: opacity .4s ease-out;
                transition: opacity .4s ease-out;
    }​    
    

提交回复
热议问题