2010-07-07 39 views
1

我用下面的代碼片段:如何使用jQuery UI顏色動畫從元素顏色更改並返回到它?

$('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()字符串。

你會如何做到這一點?

回答

0

我,如果它是存儲它的處理程序。在您提供的例如,你可以使用本地範圍的變種

不使用本地作用域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 
     });