【问题标题】:jQuery Animate not working on background-color propertyjQuery Animate 不适用于背景颜色属性
【发布时间】:2016-04-22 15:41:06
【问题描述】:

我正在玩弄 jQuery .animate() 函数,最终尝试根据用户滚动的像素数来更改 divs 之一的 background-color。令我惊讶的是,它没有用。我尝试改用.css() 函数,效果很好。请参考底部的 jsFiddle 链接。

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

jsFiddle 链接:https://jsfiddle.net/ag_dhruv/cb2sypmu/

【问题讨论】:

  • 为此使用 jQueryUI。

标签: javascript jquery css jquery-animate


【解决方案1】:

根据jQuery API docs

.animate() 方法允许我们在任何数字 CSS 属性上创建动画效果。

动画属性和值

所有动画属性都应动画为单个数值,除非以下说明;大多数非数字属性无法使用基本的 jQuery 功能进行动画处理(例如,widthheightleft 可以进行动画处理background-color 不能进行动画处理,除非使用了 jQuery.Color 插件)

重点是我

背景颜色不是数字属性,因此无法使用.animate() 对其进行动画处理。

【讨论】:

    【解决方案2】:

    如果你想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 捆绑了 jQuery Color 插件,这些插件提供了颜色动画以及许多用于处理颜色的实用函数。

    【讨论】:

    • 我的意思是没有冒犯,但我相信这个问题问为什么动画不起作用而不是替代 :)
    【解决方案3】:

    您只能使用 jQueryUI 为 backgroundColor 设置动画。 如果您不想使用 jQueryUI,您只需更改一个类并使用过渡在 CSS 中制作动画。

    【讨论】:

    • 这是作为答案发布的,但它不会尝试回答问题。它可能应该是编辑、评论、另一个问题或完全删除。
    • @PraveenKumar 这个问题没有答案,因为用 jQuery 是不可能的。
    • 哈哈...当然。但我没有标记它,只是想说这只是一个评论。
    • @lucian.nicolaescu:对此有一个答案。 OP 询问为什么会这样,所以答案是 不,jQuery animate 不支持它(不,我也没有标记这个):)
    【解决方案4】:

    只是对现有答案的补充:如果您不想为此使用 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)'})
    

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • @joe_young 是的,你完全正确。我根据您的建议编辑了答案
    【解决方案5】:

    您也可以使用 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;
    }
    

    链接到工作示例

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-12
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多