2

下手な英語でごめんなさい。

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

4 に答える 4

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);

また、アニメーション化する初期の色の値を設定する必要があります (まだ設定していない場合)。私の知る限り (間違っている可能性があります)、明示的な色ではなく適切にアニメーション化するには、色に 16 進値または rbg 値を使用する必要があります。名前。

于 2013-07-04T16:19:57.087 に答える
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;
}
于 2013-07-04T16:22:33.290 に答える
2

または、jQueryUIを使用できます

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

フィドル

于 2013-07-04T16:24:41.547 に答える
0

背景色はアニメーション化できません。このリンクを参照してください http://api.jquery.com/animate/

于 2013-07-04T16:24:34.003 に答える