【问题标题】:How do I use the jQuery UI color animation to change from an elements color and then back to it?如何使用 jQuery UI 颜色动画从元素颜色更改然后返回到它?
【发布时间】:2025-11-30 04:50:01
【问题描述】:

我正在使用以下 sn-p:

$('input[value=NEW]:hidden').attr('value', '').parent().parent().animate({
                    backgroundColor: "#FF9933",
                    duration: 800
                }).delay(500).animate({
                    duration: 800,
                    backgroundColor: '#ffffff'
                });

但是,我想将元素设置为“#FF9933”颜色,然后将其设置为更改前的颜色。

我尝试将元素背景颜色保留在 var 中并使用它,但 jQuery UI 动画似乎不喜欢 .css() 给我的 RGB() 字符串。

你会怎么做?

【问题讨论】:

    标签: jquery jquery-ui


    【解决方案1】:

    如果它在处理程序中,我会将其存储。在您提供的示例中,您可以使用本地范围的 var

    不使用本地作用域变量:

    var $parent = $('input[value=NEW]:hidden').attr('value', '').parent().parent();
    $parent.data('oldBgColor', $parent.css('backgroundColor') //save the old bg
            .animate({
                  backgroundColor: "#FF9933",
                  duration: 800
             }).delay(500).animate({
                  duration: 800,
                  backgroundColor: $parent.data('oldBgColor')
             });
    

    本地范围:

    var $parent = $('input[value=NEW]:hidden').attr('value', '').parent().parent();
    var oldBgColor = $parent.css('backgroundColor');//save the old bg - still accessible in the delayed animate 
    $parent.animate({
                  backgroundColor: "#FF9933",
                  duration: 800
             }).delay(500).animate({
                  duration: 800,
                  backgroundColor: oldBgColor
             });
    

    【讨论】: