2012-10-02 35 views
1

可能重複:
Making my ajax updated div fade inJQuery的AJAX淡入內容

我想從AJAX調用fadeIn我的網頁上的內容,但是當我使用此代碼$('#sub_container').html(content).fadeIn();它不工作,即使我將動畫速度設置爲超慢(slow(5000)

$('.create').click(function(){ 
    loadingimg(); 
    document.title = 'Create Message'; 
    $.ajax({ 
     url: 'create.php', 
     dataType: 'html', 
     success: function(content){ 
      $('#sub_container').html(content); 
      // $('#sub_container').html(content).fadeIn(); <- Fails 
     } 
    }); 
}); 
+2

重複:http://stackoverflow.com/questions/183638/making-my-ajax-updated-div-fade-in,http://stackoverflow.com/questions/8262573/ajax-jquery-fade-in -of-php-content –

回答

5

.fadeIn()函數實質上使得元素可見,所以如果元素是已經是可見,則看起來什麼也不做。您可以確保並非如此通過隱藏元素:

$('#sub_container').hide().html(content).fadeIn(); 
+0

非常感謝,只是試了一下,它的工作。 –

1

您需要確保它是隱藏的,首先,例如:

$('.create').click(function(){ 
$('#sub_container').fadeOut(); // fade out the current content 
loadingimg(); 
document.title = 'Create Message'; 
$.ajax({ 
    url: 'create.php', 
    dataType: 'html', 
    success: function(content){ 
     $('#sub_container').html(content).stop().fadeIn(); // fade in the new content 
    } 
}); 
}); 

但是,可以讓你跳的內容爲元素消失,所以如果必要的話你必須說明這一點。