How do I animate a background color to transparent in jQuery?

后端 未结 12 1793
慢半拍i
慢半拍i 2020-12-09 14:46

I can animate from transparent to color, but when I tell jquery to animate the backgroundColor: \'transparent\' it just changes to white. Any idea how to fix this?

12条回答
  •  余生分开走
    2020-12-09 15:21

    I wanted to do this and since I couldn't find it, I hacked it together. This is only for white, suit to your needs:

    function animate_bg(ele, from, to) {
        ele.css("background-color", "rgba(255, 255, 255, " + (from += from > to ? -1 : 1) / 10 + ")"); 
        if(from != to)  
            setTimeout(function() { animate_bg(ele, from, to) }, 20);
    }
    

    Usage:

    $("a").hover(
        function() {return animate_bg($(this), 0, 10)},
        function() {return animate_bg($(this), 10, 0)}
    );
    

提交回复
热议问题