2010-09-10 29 views
0

我想淡出頁面上的某些元素,使用AJAX獲取新的元素,然後淡入新的元素。淡入是好的,但淡出是行不通的。我嘗試使用fadeOut,因爲fadeIn工作正常,但淡出不起作用 - 元素消失了。我現在試圖製作不透明度變化的動畫。它工作正常的淡入這裏是代碼:使用jQuery設置淡出效果的動畫

$(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可以工作。有沒有人有任何想法,爲什麼它不淡出?

回答

1

啊 - 問題是元素在淡出完成之前被交換。我把整個AJAX函數放在animate方法的完成函數中,並且嘿!

+0

這就是要解決它。還記得任何延遲動畫的緣故(淡入,淡出等)不會停止進一步的處理,只是淡入淡出。但是你有10秒的時間了。 – 2010-09-10 23:22:32