jQuery 回调不等待淡出

发布于 2024-08-09 23:15:23 字数 798 浏览 1 评论 0原文

在我看来,以下代码应该产生这些结果:

mademoiselle
demoiselle
mesdemoiselles

相反,随着“ma”淡出,“mes”在制作序列时淡出:

mademoiselle
madesdemoiselles
mesdemoiselles

代码:

<span class="remove">ma</span><span class="add">mes</span>demoiselle<span class="add">s</span>

$(document).ready(function() {
   $(".remove").fadeOut(4000,function(){
       $(".add").fadeIn(5000);      
   });
});

编辑:我发现一个空的范围,如果我删除它会使错误消失:

<span class="remove">ma</span><span class="add">mes</span>demoiselle<span class="remove"></span><span class="add">s</span>

问题是:生成此代码的 php 代码使用 span 作为占位符。如果有必要,我会 str_replace 它们,但我很好奇为什么会发生这种情况。

It seems to me the following code should produce these results:

mademoiselle
demoiselle
mesdemoiselles

Instead, as "ma" fades out, "mes" fades in making the sequence:

mademoiselle
madesdemoiselles
mesdemoiselles

The code:

<span class="remove">ma</span><span class="add">mes</span>demoiselle<span class="add">s</span>

$(document).ready(function() {
   $(".remove").fadeOut(4000,function(){
       $(".add").fadeIn(5000);      
   });
});

Edit: I found an empty span that if I remove makes the bug go away:

<span class="remove">ma</span><span class="add">mes</span>demoiselle<span class="remove"></span><span class="add">s</span>

The problem is: The php code generating this is using the spans as placeholders. I'll str_replace them if I have to, but I'm curious why this is happening.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末骤雨初歇 2024-08-16 23:15:23

好的,我设法重现了您的问题,请参阅 http://jsbin.com/ocaha 中的示例。

发生的情况是 jQuery 可以看到您的空 不需要淡出。因此,它认为动画已完成并执行 0 毫秒,而不是预期的 4000 毫秒。因此它立即开始在 .adds全部中淡出。

为了防止这种行为,我会过滤从选择中删除所有空元素。像这样:

$(document).ready(function() {
   $(".remove")
               .filter(function(){ return ! $(this).is(":empty"); })
               .fadeOut(4000, function(){
     $(".add").fadeIn(5000);
   });
});

请参阅 http://jsbin.com/ovivi 上的工作示例。

Ok, so I managed to reproduce your problem see the example at http://jsbin.com/ocaha.

What's happening is that jQuery can see that your empty <span> does not need to be faded out. Therefor it considers the animation done and executes for 0ms instead of the expected 4000ms. So it immediately starts fading in all of the .adds.

To prevent this behaviour, I would filter away all empty elements from the selection. Like this:

$(document).ready(function() {
   $(".remove")
               .filter(function(){ return ! $(this).is(":empty"); })
               .fadeOut(4000, function(){
     $(".add").fadeIn(5000);
   });
});

See working example at http://jsbin.com/ovivi.

听闻余生 2024-08-16 23:15:23

如果不起作用,请将“:”更改为“:隐藏”:

$(document).ready(function() {
   $(".remove")
               .filter(function(){ return ! $(this).is(":hidden"); })
               .fadeOut(4000, function(){
     $(".add").fadeIn(5000);
   });
});

Change ":empty" to ":hidden" if not working:

$(document).ready(function() {
   $(".remove")
               .filter(function(){ return ! $(this).is(":hidden"); })
               .fadeOut(4000, function(){
     $(".add").fadeIn(5000);
   });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文