简体   繁体   English

jquery fadeOut() 同一个元素多次

[英]jquery fadeOut() the same element multiple times

I have the following code which notifies the user when something happens:我有以下代码在发生某些事情时通知用户:

window.parent.$(".Toast").html('Successfully Did Something!').fadeOut(9000);

It works great the first time, but then, of course, the element is still set to hidden and has the same text.第一次效果很好,但是当然,该元素仍然设置为隐藏并且具有相同的文本。 So when the user triggers it again, nothing happens.所以当用户再次触发它时,什么也没有发生。 I've tried immediately setting the html() to empty and showing it, but that happens immediately, so you never see the text.我尝试立即将 html() 设置为空并显示它,但这会立即发生,因此您永远看不到文本。 I suppose I could make it "reset" when they open the edit dialog, but I'm sure someone has a simple way around this.我想我可以在他们打开编辑对话框时将其“重置”,但我相信有人有一个简单的方法来解决这个问题。

Use .show() just before .fadeOut() :.show()之前使用.fadeOut()

window.parent.$(".Toast").html('Successfully Did Something!').show().fadeOut(9000);

To cancel the current animation, use .stop(true, true) :要取消当前的 animation,请使用.stop(true, true)

window.parent.$(".Toast").html('Successfully Did Something!').stop(true, true).show().fadeOut(9000);

there is a callback function you can add as a parameter to fadeOut.有一个回调 function 您可以将其作为参数添加到fadeOut。 It will be called once the animation is completed.一旦 animation 完成,它将被调用。 Check out the docs, here: fadeOut Docs在这里查看文档: fadeOut Docs

So, try this:所以,试试这个:

window.parent.$(".Toast").html('Successfully Did Something!').fadeOut(9000, function(){
    window.parent.$(".Toast").html('')
});

You just need to show the elements once you change the html:更改 html 后,您只需要显示元素:

window.parent.$(".Toast").html('Successfully Did Something!').show().fadeOut(9000);

but, I think that delay has the effect that you are looking for:但是,我认为延迟会产生您正在寻找的效果:

window.parent.$(".Toast").html('Successfully Did Something!').fadeIn().delay(9000).fadeOut();

If you are dealing with iframe, Can you try the below code?如果您正在处理 iframe,您可以尝试以下代码吗? It should solve your problem.它应该可以解决您的问题。

window.top.$(".Toast").html('Successfully Did Something!').fadeOut(9000, function(){
    $(this).html('').css("opacity", 1).hide();
});

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

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