简体   繁体   English

淡入淡出与CSS?

[英]FadeIn with css?

<textarea id="text" rows="5" cols="25"></textarea> <span id="fade">fade</span>

$("#fade").click(function () {  
    $("#text").fadeIn(31000, function () {   
        $("#text").css('background-color', 'red');         
    });      
    return false;    
}); 

http://jsfiddle.net/tG7xE/ http://jsfiddle.net/tG7xE/

Why in this example fadeIn doesnt working? 为什么在此示例中,fadeIn无法正常工作? How can i make it? 我该怎么做?

http://jsfiddle.net/tG7xE/5/ http://jsfiddle.net/tG7xE/5/

<textarea style="display:none;" id="text" rows="5" cols="25"></textarea> <span id="fade">fade</span>

$("#fade").click(function () {  
    $("#text").fadeIn(function () {   
        $("#text").css('background-color', 'red');         
    });      
    return false;    
}); ​

fadeIn works only if element is not visible to hide it use fadeOut() or hide() . 只有当元素不可见才能使用fadeOut()hide()将其隐藏时, fadeIn起作用。

I do think you expect to work fadeIn differently from what it actually does. 我确实认为您希望fadeIn与实际工作fadeIn不同。 It's not used to fade in the background color, but to fade in a hidden element. 它不用于淡化背景颜色,而用于淡化隐藏的元素。 In case you set your textarea 's display to none (like in: http://jsfiddle.net/tG7xE/4/ ) you'll see what I mean. 如果将textareadisplaynone (例如: http : //jsfiddle.net/tG7xE/4/ ),您将明白我的意思。 After the fadeIn is complete the specified callback is executed (bg turns red). 在fadeIn完成后,将执行指定的回调(bg变为红色)。

In case you want to animate background colors I think you should have a look at jQuery UI's possibilities to animate color values: http://jqueryui.com/demos/animate/ 如果您想设置背景颜色的动画,我认为您应该看看jQuery UI可以设置颜色值的动画: http : //jqueryui.com/demos/animate/

You need to include jQuery-UI and then execute your code like this: 您需要包括jQuery-UI,然后执行如下代码:

$("#fade").click(function() {
  $('#text').animate({backgroundColor: '#ff0000'}, 'slow');
});​

Here is an updated version of your fiddle. 是您的小提琴的更新版本。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM