Javascript jQuery动画背景色

Javascript jQuery动画背景色,javascript,jquery,colors,jquery-animate,Javascript,Jquery,Colors,Jquery Animate,我试图在mouseover上使用jQuery设置背景颜色变化的动画 我已经检查了一些例子,我似乎有它的权利,它与其他属性,如fontSize,但与我得到的背景色和“无效属性”js错误。 我使用的元素是div $(".usercontent").mouseover(function() { $(this).animate({ backgroundColor: "olive" }, "slow"); }); 有什么想法吗?给找到这个的人。最好使用jQueryUI版本,因为它适用于所有浏览器

我试图在mouseover上使用jQuery设置背景颜色变化的动画

我已经检查了一些例子,我似乎有它的权利,它与其他属性,如fontSize,但与我得到的背景色和“无效属性”js错误。 我使用的元素是div

$(".usercontent").mouseover(function() {
    $(this).animate({ backgroundColor: "olive" }, "slow");
});

有什么想法吗?

给找到这个的人。最好使用jQueryUI版本,因为它适用于所有浏览器。颜色插件与Safari和Chrome有问题。它只在某些时候起作用。

我也遇到了同样的问题,并通过包含jQuery UI解决了这个问题。以下是完整的脚本:


google.load(“jqueryui”,“1.5.2”);
$(文档).ready(函数(){
$('#menu ul li.item')。悬停(
函数(){
$(this.stop().animate({backgroundColor:'#4E1402'},300);
},函数(){
$(this.stop().animate({backgroundColor:'#943D20'},100);
});
});

如今,jQuery颜色插件支持以下命名颜色:

aqua:[0,255,255],
azure:[240,255,255],
beige:[245,245,220],
black:[0,0,0],
blue:[0,0,255],
brown:[165,42,42],
cyan:[0,255,255],
darkblue:[0,0,139],
darkcyan:[0,139,139],
darkgrey:[169,169,169],
darkgreen:[0,100,0],
darkkhaki:[189,183,107],
darkmagenta:[139,0,139],
darkolivegreen:[85,107,47],
darkorange:[255,140,0],
darkorchid:[153,50,204],
darkred:[139,0,0],
darksalmon:[233,150,122],
darkviolet:[148,0,211],
fuchsia:[255,0,255],
gold:[255,215,0],
green:[0,128,0],
indigo:[75,0,130],
khaki:[240,230,140],
lightblue:[173,216,230],
lightcyan:[224,255,255],
lightgreen:[144,238,144],
lightgrey:[211,211,211],
lightpink:[255,182,193],
lightyellow:[255,255,224],
lime:[0,255,0],
magenta:[255,0,255],
maroon:[128,0,0],
navy:[0,0,128],
olive:[128,128,0],
orange:[255,165,0],
pink:[255,192,203],
purple:[128,0,128],
violet:[128,0,128],
red:[255,0,0],
silver:[192,192,192],
white:[255,255,255],
yellow:[255,255,0]

颜色插件只比UI库便宜4kb。当然,你会希望使用插件的一部分,而不是在转换太快时不处理Safari和崩溃的插件。由于没有提供缩小版本,您可能需要测试各种压缩器和最小版本。在这种情况下,YUI只需要2317字节就可以获得最佳压缩,而且因为它太小了,所以它就是:

(function (d) {
    d.each(["backgroundColor", "borderBottomColor", "borderLeftColor", "borderRightColor", "borderTopColor", "color", "outlineColor"], function (f, e) {
        d.fx.step[e] = function (g) {
            if (!g.colorInit) {
                g.start = c(g.elem, e);
                g.end = b(g.end);
                g.colorInit = true
            }
            g.elem.style[e] = "rgb(" + [Math.max(Math.min(parseInt((g.pos * (g.end[0] - g.start[0])) + g.start[0]), 255), 0), Math.max(Math.min(parseInt((g.pos * (g.end[1] - g.start[1])) + g.start[1]), 255), 0), Math.max(Math.min(parseInt((g.pos * (g.end[2] - g.start[2])) + g.start[2]), 255), 0)].join(",") + ")"
        }
    });

    function b(f) {
        var e;
        if (f && f.constructor == Array && f.length == 3) {
            return f
        }
        if (e = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(f)) {
            return [parseInt(e[1]), parseInt(e[2]), parseInt(e[3])]
        }
        if (e = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(f)) {
            return [parseFloat(e[1]) * 2.55, parseFloat(e[2]) * 2.55, parseFloat(e[3]) * 2.55]
        }
        if (e = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(f)) {
            return [parseInt(e[1], 16), parseInt(e[2], 16), parseInt(e[3], 16)]
        }
        if (e = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(f)) {
            return [parseInt(e[1] + e[1], 16), parseInt(e[2] + e[2], 16), parseInt(e[3] + e[3], 16)]
        }
        if (e = /rgba\(0, 0, 0, 0\)/.exec(f)) {
            return a.transparent
        }
        return a[d.trim(f).toLowerCase()]
    }
    function c(g, e) {
        var f;
        do {
            f = d.css(g, e);
            if (f != "" && f != "transparent" || d.nodeName(g, "body")) {
                break
            }
            e = "backgroundColor"
        } while (g = g.parentNode);
        return b(f)
    }
    var a = {
        aqua: [0, 255, 255],
        azure: [240, 255, 255],
        beige: [245, 245, 220],
        black: [0, 0, 0],
        blue: [0, 0, 255],
        brown: [165, 42, 42],
        cyan: [0, 255, 255],
        darkblue: [0, 0, 139],
        darkcyan: [0, 139, 139],
        darkgrey: [169, 169, 169],
        darkgreen: [0, 100, 0],
        darkkhaki: [189, 183, 107],
        darkmagenta: [139, 0, 139],
        darkolivegreen: [85, 107, 47],
        darkorange: [255, 140, 0],
        darkorchid: [153, 50, 204],
        darkred: [139, 0, 0],
        darksalmon: [233, 150, 122],
        darkviolet: [148, 0, 211],
        fuchsia: [255, 0, 255],
        gold: [255, 215, 0],
        green: [0, 128, 0],
        indigo: [75, 0, 130],
        khaki: [240, 230, 140],
        lightblue: [173, 216, 230],
        lightcyan: [224, 255, 255],
        lightgreen: [144, 238, 144],
        lightgrey: [211, 211, 211],
        lightpink: [255, 182, 193],
        lightyellow: [255, 255, 224],
        lime: [0, 255, 0],
        magenta: [255, 0, 255],
        maroon: [128, 0, 0],
        navy: [0, 0, 128],
        olive: [128, 128, 0],
        orange: [255, 165, 0],
        pink: [255, 192, 203],
        purple: [128, 0, 128],
        violet: [128, 0, 128],
        red: [255, 0, 0],
        silver: [192, 192, 192],
        white: [255, 255, 255],
        yellow: [255, 255, 0],
        transparent: [255, 255, 255]
    }
})(jQuery);
您可以使用2个div:

你可以在上面放一个克隆,然后在淡入克隆的同时淡出原稿

淡出完成后,使用新背景恢复原稿

$(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);
    });
});​




我喜欢使用delay()来完成这项工作,下面是一个示例:

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

这可以由函数调用,其中“element”是元素类/名称/等。元素将立即以#FCFCD8背景出现,保持一秒钟,然后淡入#EFEAEA

ColorBlend插件正是您想要的

这是我的突出显示代码

$("#container").colorBlend([{
    colorList:["white",  "yellow"], 
    param:"background-color",
    cycles: 1,
    duration: 500
}]);

Bitstorm有我见过的最好的jquery彩色动画插件。这是对jquery颜色项目的改进。它还支持rgba

试试这个:

jQuery(".usercontent").hover(function() {
    jQuery(this).animate({backgroundColor:"pink"}, "slow");
},function(){
    jQuery(this).animate({backgroundColor:"white"}, "slow");
});
(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);  
修订方式及效果:

jQuery(".usercontent").hover(function() {

    jQuery(this).fadeout("slow",function(){
        jQuery(this).animate({"color","yellow"}, "slow");
    });
});

您可以使用jQuery UI添加此功能。你可以抓取你所需要的,所以如果你想给颜色设置动画,你只需要包含以下代码。我从最新的jQueryUI(目前为1.8.14)获得了if

使用YUI压缩后仅为1.43kb:

$.each(["backgroundColor","borderBottomColor","borderLeftColor","borderRightColor","borderTopColor","borderColor","color","outlineColor"],function(b,a){$.fx.step[a]=function(c){if(!c.colorInit){c.start=getColor(c.elem,a);c.end=getRGB(c.end);c.colorInit=true}c.elem.style[a]="rgb("+Math.max(Math.min(parseInt((c.pos*(c.end[0]-c.start[0]))+c.start[0],10),255),0)+","+Math.max(Math.min(parseInt((c.pos*(c.end[1]-c.start[1]))+c.start[1],10),255),0)+","+Math.max(Math.min(parseInt((c.pos*(c.end[2]-c.start[2]))+c.start[2],10),255),0)+")"}});function getRGB(b){var a;if(b&&b.constructor==Array&&b.length==3){return b}if(a=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(b)){return[parseInt(a[1],10),parseInt(a[2],10),parseInt(a[3],10)]}if(a=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(b)){return[parseFloat(a[1])*2.55,parseFloat(a[2])*2.55,parseFloat(a[3])*2.55]}if(a=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(b)){return[parseInt(a[1],16),parseInt(a[2],16),parseInt(a[3],16)]}if(a=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(b)){return[parseInt(a[1]+a[1],16),parseInt(a[2]+a[2],16),parseInt(a[3]+a[3],16)]}if(a=/rgba\(0, 0, 0, 0\)/.exec(b)){return colors.transparent}return colors[$.trim(b).toLowerCase()]}function getColor(c,a){var b;do{b=$.curCSS(c,a);if(b!=""&&b!="transparent"||$.nodeName(c,"body")){break}a="backgroundColor"}while(c=c.parentNode);return getRGB(b)};
您还可以使用CSS3转换设置颜色动画,但它仅受现代浏览器支持

a.test {
  color: red;
  -moz-transition-property: color;  /* FF4+ */
  -moz-transition-duration: 1s;
  -webkit-transition-property: color;  /* Saf3.2+, Chrome */
  -webkit-transition-duration: 1s;
  -o-transition-property: color;  /* Opera 10.5+ */
  -o-transition-duration: 1s;
  -ms-transition-property: color;  /* IE10? */
  -ms-transition-duration: 1s;
  transition-property: color;  /* Standard */
  transition-duration: 1s;
  }

  a.test:hover {
  color: blue;
  }
使用速记属性:

/* shorthand notation for transition properties */
/* transition: [transition-property] [transition-duration] [transition-timing-function] [transition-delay]; */

a.test {
  color: red;
  -moz-transition: color 1s;
  -webkit-transition: color 1s;
  -o-transition: color 1s;
  -ms-transition: color 1s;
  transition: color 1s;
  }

a.test {
  color: blue;
 }
与常规javascript转换不同,CSS3转换是硬件加速的,因此更平滑。您可以使用Modernizr来确定浏览器是否支持CSS3转换,如果不支持,则可以使用jQuery作为后备:

if ( !cssTransitions() ) {
    $(document).ready(function(){
        $(".test").hover(function () {
                $(this).stop().animate({ backgroundColor: "red" },500)
             }, function() {
                 $(this).stop().animate({ backgroundColor: "blue" },500)}    
             );
    }); 
}
请记住在开始新动画之前使用stop()停止当前动画,否则当您过快地通过元素时,效果会持续闪烁一段时间。

尝试使用它

-moz-transition: background .2s linear;
-webkit-transition: background .2s linear;
-o-transition: background .2s linear;
transition: background .2s linear;

我偶然发现这一页上有同样的问题,但有以下问题:

  • 我不能在当前设置中包含额外的jQuery插件文件
  • 我不喜欢粘贴我没有时间阅读和验证的大块代码
  • 我没有访问css的权限
  • 我几乎没有时间实施(这只是对管理页面的视觉改进)
  • 由于上述原因,几乎排除了所有答案。考虑到我的褪色非常简单,我使用了以下快速破解:

    element
      .css('color','#FF0000')
    ;
    $('<div />')
      .css('width',0)
      .animate(
        {'width':100},
        {
          duration: 3000,
          step:function(now){
            var v = (255 - 255/100 * now).toString(16);
            v = (v.length < 2 ? '0' : '') + v.substr(0,2);
            element.css('color','#'+v+'0000');
          }
        }
      )
    ;
    
    元素
    .css('color','#FF0000'))
    ;
    $('')
    .css('width',0)
    .制作动画(
    {'width':100},
    {
    时长:3000,
    步骤:功能(现在){
    var v=(255-255/100*now).toString(16);
    v=(v.length<2?'0':'')+v.substr(0,2);
    css('color','#'+v+'0000');
    }
    }
    )
    ;
    
    上面创建的临时div永远不会放在文档流中。然后,我使用jQuery的内置动画来设置该元素的数值属性的动画-在本例中为
    width
    -它可以表示一个百分比(0到100)。然后,使用step函数,我通过简单的十六进制计算将这个数字动画转换为文本颜色

    使用
    setInterval
    也可以实现同样的效果,但是通过使用此方法,您可以受益于jQuery的动画方法,如
    .stop()
    ,并且可以使用
    easing
    duration


    很明显,它只适用于简单的颜色渐变,对于更复杂的颜色转换,您需要使用上面的答案之一——或者编写您自己的颜色渐变数学:)

    为了达到所需的效果,我将CSS转换与JQuery结合使用;显然,不支持CSS转换的浏览器将不会设置动画,但它是一个轻量级的选项,适用于大多数浏览器和我的需求,这是可以接受的

    Jquery更改背景颜色:

       $('.mylinkholder a').hover(
            function () {
                $(this).css({ backgroundColor: '#f0f0f0' }); 
            },
            function () {
                $(this).css({ backgroundColor: '#fff' });
            }
        );
    
    CSS使用过渡到淡入淡出背景颜色更改

       .mylinkholder a
       {
       transition: background-color .5s ease-in-out;
       -moz-transition: background-color .5s ease-in-out;
       -webkit-transition: background-color .5s ease-in-out; 
      -o-transition: background-color .5s ease-in-out; 
       }
    

    如果不想仅使用核心jQuery功能设置背景动画,请尝试以下操作:

    jQuery(".usercontent").mouseover(function() {
          jQuery(".usercontent").animate({backgroundColor:'red'}, 'fast', 'linear', function() {
                jQuery(this).animate({
                    backgroundColor: 'white'
                }, 'normal', 'linear', function() {
                    jQuery(this).css({'background':'none', backgroundColor : ''});
                });
            });
    

    使用CSS3转换来实现。支持非常好(所有现代浏览器,甚至IE)。使用指南针和SASS,可以快速完成:

    #foo {background:red; @include transition(background 1s)}
    #foo:hover {background:yellow}
    
    纯CSS:

    #foo {
    background:red;
    -webkit-transition:background 1s;
    -moz-transition:background 1s;
    -o-transition:background 1s;
    transition:background 1s
    }
    #foo:hover {background:yellow}
    
    我写了一篇关于这个话题的德语文章:

    试试这个:

    jQuery(".usercontent").hover(function() {
        jQuery(this).animate({backgroundColor:"pink"}, "slow");
    },function(){
        jQuery(this).animate({backgroundColor:"white"}, "slow");
    });
    
    (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);  
    

    您可以在此处预览示例:

    只需在jquery脚本下面添加以下代码片段,即可享受:

    <script src="https://cdn.jsdelivr.net/jquery.color-animation/1/mainfile"></script>
    
    
    


    请您引用一下来源。谢谢你的列表。这个列表来自jQuery颜色插件:-1你的颜色列表指的是一个过时的版本。我注意到,当前版本至少有一种额外的颜色。-1最新版本的颜色插件在Chrome中工作得非常完美。添加额外的scri非常重要