jQuery animate to transparent

前端 未结 9 1918
迷失自我
迷失自我 2021-01-18 06:05
$(\".block li\").hover(
    function(){
        $(this).animate({backgroundColor: \"#000\"});
    },
    function(){
        $(this).animate({backgroundColor: \"#fff         


        
9条回答
  •  隐瞒了意图╮
    2021-01-18 06:43

    You could use rgba(...) (see browser support here).

    var elem = $('#foo')[0];
    
    $({
        r: 0,
        g: 0,
        b: 0,
        a: 1
    }).animate({
        a: 0
    }, {
        step: function() {
            elem.style.backgroundColor =
                'rgba(' +
                    ~~this.r + ',' + ~~this.g + ',' + ~~this.b + ',' +
                    ~~(this.a*100)/100 +
                ')';
        },
        duration: 1000
    });​
    

    ~~ is to floor values, otherwise you'll end up with stuff like rgba(0.1029302....

提交回复
热议问题