2013-07-04 62 views
2

对不起,我的英文不好的动画功能。愚蠢的问题与jQuery

动画效果 jQuery的功能在第2行工作正常。但在第3行不起作用...为什么?

$('.opac').hover(function(){ 
    $('img', this).animate({opacity: '0.6'}, 500); 
    $('p', this).animate({background: 'orange'}, 500); 
    }); 

的HTML是这样的:

<div class="opac"> 
    <img src="image.png" /> 
    <p class="fot">text here...</p> 
    </div> 

谢谢! PS:开发工具不提供任何错误...

+1

现在您发布的jQuery代码是有语法错误。这是实际的代码吗?可能这就是为什么代码不工作 –

+1

你缺少墙裙伤心的话);在函数结束时。也许这将也有助于> http://stackoverflow.com/questions/190560/jquery-animate-backgroundcolor –

+0

哦,我写的不对的代码。现在是对的。但原来的代码是正确的,不再工作... – CastenettoA

回答

5

jQuery的本身不支持动画的色彩。下面是一个简单的插件在你的代码库,包括:

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

(function(a){function b(){var b=a("script:first"),c=b.css("color"),d=!1;if(/^rgba/.test(c))d=!0;else try{d=c!=b.css("color","rgba(0, 0, 0, 0.5)").css("color"),b.css("color",c)}catch(e){}return d}function c(b,c,d){var e="rgb"+(a.support.rgba?"a":"")+"("+parseInt(b[0]+d*(c[0]-b[0]),10)+","+parseInt(b[1]+d*(c[1]-b[1]),10)+","+parseInt(b[2]+d*(c[2]-b[2]),10);return a.support.rgba&&(e+=","+(b&&c?parseFloat(b[3]+d*(c[3]-b[3])):1)),e+=")"}function d(a){var b,c;return(b=/#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})/.exec(a))?c=[parseInt(b[1],16),parseInt(b[2],16),parseInt(b[3],16),1]:(b=/#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/.exec(a))?c=[17*parseInt(b[1],16),17*parseInt(b[2],16),17*parseInt(b[3],16),1]:(b=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(a))?c=[parseInt(b[1]),parseInt(b[2]),parseInt(b[3]),1]:(b=/rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9\.]*)\s*\)/.exec(a))&&(c=[parseInt(b[1],10),parseInt(b[2],10),parseInt(b[3],10),parseFloat(b[4])]),c}a.extend(!0,a,{support:{rgba:b()}});var e=["color","backgroundColor","borderBottomColor","borderLeftColor","borderRightColor","borderTopColor","outlineColor"];a.each(e,function(b,e){a.Tween.propHooks[e]={get:function(b){return a(b.elem).css(e)},set:function(b){var f=b.elem.style,g=d(a(b.elem).css(e)),h=d(b.end);b.run=function(a){f[e]=c(g,h,a)}}}}),a.Tween.propHooks.borderColor={set:function(b){var f=b.elem.style,g=[],h=e.slice(2,6);a.each(h,function(c,e){g[e]=d(a(b.elem).css(e))});var i=d(b.end);b.run=function(b){a.each(h,function(a,d){f[d]=c(g[d],i,b)})}}}})(jQuery); 

您还需要设置初始颜色值从动画(如果你还没有的话),而据我所知(可能是错误的),您应该使用十六进制或rbg值为您的颜色正确动画,而不是显式颜色名称。

+1

也可能与[jQuery UI](http://jqueryui.com/animate/) – Alvaro

+0

哦,我不知道。非常感谢!! :) – CastenettoA

3

如果你不反对CSS3:http://jsfiddle.net/MkgDC/1/

img { 
    opacity: 1; 
    -webkit-transition: all .5s ease; 
    -moz-transition: all .5s ease; 
    -ms-transition: all .5s ease; 
    -o-transition: all .5s ease; 
    transition: all .5s ease; 
} 
p { 
    -webkit-transition: all .5s ease; 
    -moz-transition: all .5s ease; 
    -ms-transition: all .5s ease; 
    -o-transition: all .5s ease; 
    transition: all .5s ease; 
} 
.opac:hover > img { 
    opacity: .6; 
} 
.opac:hover > p { 
    background: orange; 
} 
+0

是的,我知道...而且太棒了。但是我不能在开发中使用它 – CastenettoA

0

背景颜色不能动画。看到此链接

2

或者你可以使用jQueryUI的

$('div.opac').hover(function(){ 
    jQuery('img', this).animate({opacity: '0.6'}, 500); 
    jQuery('p', this).animate({backgroundColor: "#aa0000"}, 500); 
    }); 

Fiddle