我试图在鼠标悬停时使用jQuery动画改变backgroundColor。

我检查了一些例子,我似乎有它是正确的,它与其他属性,如fontSize,但与backgroundColor我得到和“无效属性”js错误。 我正在使用的元素是div。

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

什么好主意吗?


当前回答

我也有同样的问题,并通过包含jQuery UI来修复它。以下是完整的脚本:

<!-- include Google's AJAX API loader -->
<script src="http://www.google.com/jsapi"></script>
<!-- load JQuery and UI from Google (need to use UI to animate colors) -->
<script type="text/javascript">
google.load("jqueryui", "1.5.2");
</script>


<script type="text/javascript">
$(document).ready(function() {
$('#menu ul li.item').hover(
    function() {
        $(this).stop().animate({backgroundColor:'#4E1402'}, 300);
        }, function () {
        $(this).stop().animate({backgroundColor:'#943D20'}, 100);
    });
});
</script>

其他回答

ColorBlend插件完全是你想要的

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

这里是我的高亮代码

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

为了达到理想的效果,我使用了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; 
   }

试试这个:

(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);  

你可以在这里预览示例:http://jquerydemo.com/demo/jquery-animate-background-color.aspx

你可以使用2个div:

你可以在上面放一个复制品,然后在把复制品淡入的同时把原来的淡出。

当淡出完成后,用新的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例子

.toggleClass() .offset() .fadeIn() .淡出()

简单地添加以下片段下面的jquery脚本和享受:

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

参见示例

更多信息参考