简体   繁体   English

jQuery是否可以多次使用.fadeTo()?

[英]jQuery Possible to use the .fadeTo() more than once?

I am trying to write a script so that every time a specific button is pressed, I want to send out a warning message in a div tag. 我正在尝试编写脚本,以便每次按下特定按钮时,都希望在div标签中发送警告消息。 I decided to use the fadeTo because my div tag is inside a table and I dont want it to collapse. 我决定使用fadeTo,因为div标签位于表中,并且我不希望它折叠。 However, it seems like after the first press, any subsequent press will not work anymore. 但是,似乎在第一次按下后,以后的任何按下都将不再起作用。 Does anyone know a workaround for this? 有谁知道解决方法?

Here's my warning code: 这是我的警告代码:

$("#warning").html("The value is too low!").css('color','red').fadeTo(10000,0);

At the end of the fadeTo , the element's opacity is 0 . fadeTo ,元素的不透明度为0 To re-start, reset the opacity back to 100, since otherwise it's trying to animate from 0 to 0 , which is...subtle to say the least. 要重新启动,请将不透明度重新设置为100,因为否则它将尝试从00进行动画处理,这至少可以说是微妙的。 :-) :-)

Eg: 例如:

$("#warning")
    .html("The value is too low!")
    .stop()            // <=== New bit: Stop the animation if it's running
    .css({
      'opacity': 1,    // <=== New bit: Reset the opacity
      'color': 'red'
    })
    .fadeTo(10000,0);

Live example | 现场示例 | source (uses one second rather than ten) (使用一秒而不是十秒)

您将不得不再次显示div:

$("#warning").show().html("The value is too low!").css('color','red').fadeTo(10000,0);

Another way is to use opacity animation instead: 另一种方法是改用不透明度动画:

$("#warning").html("The value is too low!").css({
    color : 'red',
    opacity : 1
}).stop().animate({
    opacity : 0
}, 1000);

DEMO: http://jsfiddle.net/SDWmn/1/ 演示: http : //jsfiddle.net/SDWmn/1/

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

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