jQuery animate backgroundColor

前端 未结 17 2085
醉梦人生
醉梦人生 2020-11-21 07:11

I am trying to animate a change in backgroundColor using jQuery on mouseover.

I have checked some example and I seem to have it right, it works with other properties

相关标签:
17条回答
  • 2020-11-21 07:56

    Try this one:

    (function($) {  
    
                var i = 0;  
    
                var someBackground = $(".someBackground");  
                var someColors = [ "yellow", "red", "blue", "pink" ];  
    
    
                someBackground.css('backgroundColor', someColors[0]);  
    
                window.setInterval(function() {  
                    i = i == someColors.length ? 0 : i;  
                    someBackground.animate({backgroundColor: someColors[i]}, 3000);  
                    i++;  
                }, 30);  
    
    })(jQuery);  
    

    you can preview example here: http://jquerydemo.com/demo/jquery-animate-background-color.aspx

    0 讨论(0)
  • 2020-11-21 07:57

    For anyone finding this. Your better off using the jQuery UI version because it works on all browsers. The color plugin has issues with Safari and Chrome. It only works sometimes.

    0 讨论(0)
  • 2020-11-21 08:00

    You can use 2 divs:

    You could put a clone on top of it and fade the original out while fading the clone in.

    When the fades are done, restore the original with the new bg.

    $(function(){
        var $mytd = $('#mytd'), $elie = $mytd.clone(), os = $mytd.offset();
    
          // Create clone w other bg and position it on original
        $elie.toggleClass("class1, class2").appendTo("body")
             .offset({top: os.top, left: os.left}).hide();
    
        $mytd.mouseover(function() {            
              // Fade original
            $mytd.fadeOut(3000, function() {
                $mytd.toggleClass("class1, class2").show();
                $elie.toggleClass("class1, class2").hide();            
            });
              // Show clone at same time
            $elie.fadeIn(3000);
        });
    });​
    

    jsFiddle example


    .toggleClass()
    .offset()
    .fadeIn()
    .fadeOut()

    0 讨论(0)
  • 2020-11-21 08:00

    I like using delay() to get this done, here's an example:

    jQuery(element).animate({ backgroundColor: "#FCFCD8" },1).delay(1000).animate({ backgroundColor: "#EFEAEA" }, 1500);
    

    This can be called by a function, with "element" being the element class/name/etc. The element will instantly appear with the #FCFCD8 background, hold for a second, then fade into #EFEAEA.

    0 讨论(0)
  • 2020-11-21 08:00

    ColorBlend plug in does exactly what u want

    http://plugins.jquery.com/project/colorBlend

    Here is the my highlight code

    $("#container").colorBlend([{
        colorList:["white",  "yellow"], 
        param:"background-color",
        cycles: 1,
        duration: 500
    }]);
    
    0 讨论(0)
提交回复
热议问题