jQuery animate to transparent

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


        
相关标签:
9条回答
  • 2021-01-18 06:28

    Edit: I have tested this and it works.

    Create two classes. One with background: #000 and one with background: transparent;

    Animate the toggleClass or removeClass for the #000 background class.

    example:

    jQuery('.block li').hover(function() {
      $(this).toggleClass('blackClass', 'fast' );
    }
    

    CSS:

    .blackClass { background: #000; }
    
    0 讨论(0)
  • 2021-01-18 06:30

    Instead of changing background color, remove that attribute!

    The code is as simple as:

    $("li").hover(
        function(){
            $(this).toggleClass('hover', 1000);
        },
        function(){
            $(this).toggleClass('hover', 2000);
        }
    );
    

    Example: http://jsfiddle.net/rdWTE/

    For it to work, you need jQuery and jQuery UI. Does exactly what you wanted (except the colors)!

    Those numbers in jQuery script stand for animation duration in milliseconds.

    EDIT:

    Uhm... Found out that toggleClass can bug from time to time. Better to use addClass on hover, and removeClass on mouse out.

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

    I got a solution to this issue from this link

    $('#test').animate({
    
    backgroundColor: "#DFC21D",
    
    }, 1000, function() {
    
    $('#test').css("backgroundColor","transparent");
    
    });
    
    0 讨论(0)
  • 2021-01-18 06:38

    $(".block li").hover( function(){ $(this).animate({backgroundColor: "#000"}); }, function(){ $(this).animate({backgroundColor: $(this).parent().css("backgroundColor") }); } );

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

    This may require some change to your HTML and CSS (which could be effected by script if you don't have direct HTML/CSS control).

    I would split each '.block li' element into two elements: one that will be the background element that can be animated with $.fadeTo(), and another element laid over the top that will contain your foreground content and on which you can call $.hover() with your handlers.

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

    I think you need to use the color plugin.

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