jQuery的animate函数的愚蠢问题

时间:2013-07-04 16:14:34

标签: javascript jquery jquery-animate

抱歉英语不好。

jQuery的 animate 函数在第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:开发人员工具不会给出任何错误......

4 个答案:

答案 0 :(得分: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 :(得分: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;
}

答案 2 :(得分:2)

或者您可以使用 jQueryUI

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

Fiddle

答案 3 :(得分:0)

背景颜色无法设置动画。看到这个链接 http://api.jquery.com/animate/