jQuery animate to transparent - easier solution?

后端 未结 2 898
小鲜肉
小鲜肉 2021-01-27 15:47

I want to animate background from coloured to transparent. I have code like this:

$(\'.selector\')
.animate({
        \'background-color\' : \'transparent\'
}, 2         


        
相关标签:
2条回答
  • 2021-01-27 16:17

    css

    .animated {-webkit-transition:background 0.4s ease;-moz-transition:background 0.5s ease;-ms-background 0.5s ease;background 0.5s ease;}
    .selector {background:red}
    .transparent_background {background: transparent};
    

    html

    <div class="selector animated"></diV>
    

    js

    $(".selector").addClass("transparent_background");
    

    http://jsfiddle.net/UEyc6/

    0 讨论(0)
  • 2021-01-27 16:17

    Let CSS do the animation, and fallback to javascript when it's not supported with (for example) Modernizr: http://modernizr.com/

    if (Modernizr.csstransitions) {
        // Animate background color
        $('.element').addClass('animate-background-color');
    } else {
        // use javascript animation.
    }
    

    And Your css:

    .element {
        -webkit-transition: background-color .5s ease-out 0.1s;
        -moz-transition: background-color 5s ease-out 0.1s;
        transition: background-color .5s ease-out 0.1s;
        background-color: rgba(121,123,123,1);
    }
    
    .element.animate-background-color {
        background-color: rgba(121,123,123,0);
    }
    

    Fiddle for the css animation: http://jsfiddle.net/c5PHf/

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