jQuery 动画不适用于背景色 属性

jQuery Animate not working on background-color property

我在玩 jQuery .animate() 函数,最后尝试根据 div 的数量更改其中一个 background-color用户滚动的像素。令我惊讶的是,它没有用。我尝试改用 .css() 函数,效果很好。请参考底部的 jsFiddle link。

有人可以向我解释为什么会这样吗?

jsFiddle link : https://jsfiddle.net/ag_dhruv/cb2sypmu/

根据jQuery API docs

The .animate() method allows us to create animation effects on any numeric CSS property.

Animation Properties and Values

All animated properties should be animated to a single numeric value, except as noted below; most properties that are non-numeric cannot be animated using basic jQuery functionality (For example, width, height, or left can be animated but background-color cannot be, unless the jQuery.Color plugin is used)

重点是我的

背景颜色不是数字 属性,因此无法使用 .animate() 设置动画。

如果您想 animate background-color 属性,您需要包含 jQueryUI 或添加此插件:

$(function() {
  var state = true;
  $("#button").click(function() {
    if (state) {
      $("#effect").animate({
        backgroundColor: "#aa0000",
        color: "#fff",
        width: 500
      }, 1000);
    } else {
      $("#effect").animate({
        backgroundColor: "#fff",
        color: "#000",
        width: 240
      }, 1000);
    }
    state = !state;
  });
});
.toggler {
  width: 500px;
  height: 200px;
  position: relative;
}
#button {
  padding: .5em 1em;
  text-decoration: none;
}
#effect {
  width: 240px;
  height: 135px;
  padding: 0.4em;
  position: relative;
  background: #fff;
}
#effect h3 {
  margin: 0;
  padding: 0.4em;
  text-align: center;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="toggler">
  <div id="effect" class="ui-widget-content ui-corner-all">
    <h3 class="ui-widget-header ui-corner-all">Animate</h3>
    <p>
      Etiam libero neque, luctus a, eleifend nec, semper at, lorem. Sed pede. Nulla lorem metus, adipiscing ut, luctus sed, hendrerit vitae, mi.
    </p>
  </div>
</div>
<button id="button" class="ui-state-default ui-corner-all">Toggle Effect</button>

jQuery UI bundles the jQuery Color plugins which provides color animations as well as many utility functions for working with colors.

您只能使用 jQueryUI 为背景颜色设置动画。 如果您不想使用 jQueryUI,您只需更改 class 并使用过渡在 CSS 中制作动画。

只是对现有答案的补充:如果您不想为此使用 jQuery UI,您可以使用此 jQuery 插件(仅限 2.7kB):

http://www.bitstorm.org/jquery/color-animation/

您可以从项目的网站下载 jquery.animate-colors.jsjquery.animate-colors.min.js,或从 CDN 包含它:

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

包含后您可以通过以下方式使用彩色动画:

$('#demodiv').animate({color: '#E4D8B8'})
$('#demodiv').animate({backgroundColor: '#400101'})
$('#demodiv').animate({borderBottomColor: '#00346B'})
$('#demodiv').animate({borderColor: 'darkolivegreen'})
$('#demodiv').animate({color: 'rgba(42, 47, 76, 0.1)'})

您还可以使用 jQuery 为背景颜色添加动画效果,只是不使用 animate() 方法,如您所见,css 方法即可。

因为这是一件很容易的事情,所以不要在代码中包含其他库,省去 http 请求,只用普通的 JS 来完成。

这个函数就可以了:

function changeBG(el,clr){
var elem = document.getElementById(el);
elem.style.transition = "background 1.0s linear 0s";
elem.style.background = clr;
}

Link 到工作示例

http://codepen.io/damianocel/pen/wMKowa