2011-01-15 3 views
4

background-color이 작동하지 않기 때문에 jQuery에서 애니메이션을 적용 할 수 있습니까?Jquery background animate

$('#something').animate({ 
    background :"red" 
}, 1000); 
+0

가 실제로 어떻게 원활 사업부의 배경을 변경하려면 어떻게해야합니까 (그리고 jQuery.Color 플러그인을 사용하지 않는)입니까 ?? –

+0

질문에 대한 부록을 만들고 싶다면 의견을 편집하는 대신 편집 할 수 있습니다. 질문의 왼쪽 하단에있는 ['edit'] (http://stackoverflow.com/posts/4701156/edit) 링크를 클릭하기 만하면됩니다. –

+0

'background'는'background-color','background-image','background-position','background-repeat','background-attachment'를 정의하는 속기 표기법입니다. 귀하의 질문에 고정하려고하는 속성은'background-color'입니다 (또는 따옴표없이 JS에서는'backgroundColor'). –

답변

2

당신은 jQuery를 함께 컬러 애니메이션을 할 수있는 플러그인이 필요합니다 다음 docs에서

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

+0

+1. 이. [API 문서 (http://api.jquery.com/animate)에 따라 : _ "숫자가 아닌 속성은 기본 jQuery 기능을 사용하여 애니메이션으로 만들 수 없습니다 (예 : 'width','height' 또는 'left'는 움직일 수 있지만'background-color'는 될 수 없습니다.) "_ –

5

,

The jQuery UI project extends the .animate() method by allowing some non-numeric styles such as colors to be animated. The project also includes mechanisms for specifying animations through CSS classes rather than individual attributes.

당신이 jQuery를 UI를 사용하는 경우 그래서, 당신은 애니메이션을 적용 할 수 있습니다 배경 색상. backgroundColor을 사용하고 background이 아닌지 확인하십시오.

jQuery의 경우 color animations plugin도 사용합니다.

라이브 예 : http://jsfiddle.net/thai/NXejr/2/

1

이 시도 :

$('#something').animate({ backgroundColor: "#FF0000" }, 1000, null, function() { 
     $('#something').css("backgroundColor", "#FF0000"); 
    }); 

나는 애니메이션과 다양한 성공하지만, 발견 했어 사용하여 콜백 내장 플러스 jQuery의 CSS는 대부분의 경우에 작동하는 것 같다. IE9, FF4, Chrome에서 jQuery 1.5를 사용하여 테스트되었습니다. 보다 완벽한 솔루션을

은이 플러그인을 추가

$(document).ready(function() { 

    $.fn.animateHighlight = function (highlightColor, duration) { 
     var highlightBg = highlightColor || "#FF0000"; 
     var animateMs = duration || 1000; 
     var originalBg = this.css("background-color"); 

     if (!originalBg || originalBg == highlightBg) 
      originalBg = "#FFFFFF"; // default to white 

     jQuery(this) 
      .css("backgroundColor", highlightBg) 
      .animate({ backgroundColor: originalBg }, animateMs, null, function() { 
       jQuery(this).css("backgroundColor", originalBg); 
      }); 
    }; 
}); 

을하고 그래서 같이 호출 :

$('#something').animateHighlight(); 
0

당신은 같은 효과 CSS3 전환을 사용할 수 있습니다. 당신의 애니메이션이 솔루션

#something { 
    -webkit-transition: background-color 1s linear; 
    transition: background-color 1s linear; 
    background: red; 

} 

유일한 문제 achived 수 IE < 10 지원

0

유사한 구문 (...{background-color :"red"}...)를 사용하여, 나는 오류

SyntaxError: Unexpected token -

내가 할 수 있었다를 얻을 작은 따옴표로 CSS 속성 background-color을 캡슐화하여 작동 시키십시오.

$('#something').animate({ 
    'background-color' :"red" 
}, 1000); 
1

이 순수 jQuery를 사용하여 RGB 색상을 애니메이션 간단한 코드는

var start = [255, 0, 0], end=[255,255,255]; 
$('#element').animate({'aaa': 1}, {step: function(now){ 
    $(this).css('background-color', 'rgb('+ 
     parseInt(start[0] + (end[0]-start[0]) * now) + ',' + 
     parseInt(start[1] + (end[1]-start[1]) * now) + ',' + 
     parseInt(start[2] + (end[2]-start[2]) * now) + ')' 
    ) 
}, complete: function(){$(this).css('aaa', 0)}}) 
+0

고마워요! 그것은 나를 위해 작동합니다. –