使用 jQuery 制作淡出动画时遇到问题

发布于 2024-09-19 02:00:36 字数 1053 浏览 6 评论 0原文

我正在尝试淡出页面上的一些元素,使用 AJAX 获取新元素,然后淡入新元素。淡入效果很好,但淡出效果不行。我尝试使用 fadeOut,因为 fadeIn 工作正常,但淡出根本不起作用 - 元素就消失了。我现在正在尝试制作不透明度变化的动画。它对于淡入效果很好。代码如下:

$(document).ready(function() {
   setTimeout("getTestimonial()", 10000);
});

   function getTestimonial() {
     var counter = $('#products #cart-widget .counter').html();
        $('#products #cart-widget p > span').each(function(index) {
           if($(this).is('.counter')) {
           } else {
              $(this).animate({opacity: 0}, 5000, function(){});
           }
        });
    $.get("testimonials_include.php5", {'counter':counter}, function(data) {
       $('#products #cart-widget p').replaceWith(data);
       $('#products #cart-widget p').children().css("opacity",0);
$('#products #cart-widget p > span').each(function(index) {
   if($(this).is('.counter')) {
   } else {
      $(this).animate({opacity: 1}, 5000, function(){});
   }
});
    });
    setTimeout("getTestimonial()", 10000);
   }

请注意,新元素的不透明度默认为 1,因此我必须将它们设置为 0,然后淡入才能起作用。有谁知道为什么它不消失?

I'm trying to fade out some elements on a page, fetch new ones with AJAX, and then fade in the new ones. The fade in is fine, but the fadeout just won't work. I tried using fadeOut, because fadeIn worked fine, but the fadeout simply wouldn't work - the elements just vanished. I'm now trying to animate an opacity change. It works fine for the fade in. Here is the code:

$(document).ready(function() {
   setTimeout("getTestimonial()", 10000);
});

   function getTestimonial() {
     var counter = $('#products #cart-widget .counter').html();
        $('#products #cart-widget p > span').each(function(index) {
           if($(this).is('.counter')) {
           } else {
              $(this).animate({opacity: 0}, 5000, function(){});
           }
        });
    $.get("testimonials_include.php5", {'counter':counter}, function(data) {
       $('#products #cart-widget p').replaceWith(data);
       $('#products #cart-widget p').children().css("opacity",0);
$('#products #cart-widget p > span').each(function(index) {
   if($(this).is('.counter')) {
   } else {
      $(this).animate({opacity: 1}, 5000, function(){});
   }
});
    });
    setTimeout("getTestimonial()", 10000);
   }

Note that the new elements' opacity was by default 1, so I had to set them to 0 before the fade in could work. Does anyone have any ideas why it isn't fading out?

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

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

发布评论

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

评论(1

口干舌燥 2024-09-26 02:00:36

啊 - 问题是元素在淡入淡出完成之前就被交换了。我将整个 AJAX 函数放在 animate 方法的完成函数中,嘿,快点!

Ah - the problem was that the elements were being swapped before the fade could complete. I put the entire AJAX function in the completion function for the animate method and hey presto!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文