jQuery animate to transparent

前端 未结 9 1920
迷失自我
迷失自我 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....

    0 讨论(0)
  • 2021-01-18 06:50

    Why not use the opacity CSS3 tag?

    0 讨论(0)
  • 2021-01-18 06:53

    This might work. It prepends a new div with a background color onMouseOver, then it fades out and removes the div onMouseOut.

    Example.

    Example with list items over an image.

    Good luck, hope this helps.

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