2013-02-05 129 views
1

我想用ajax封裝div內容,但在隱藏它之前和更改之後,用jQuery動畫顯示它。jQuery隱藏,更改內容,顯示

我當前的代碼:

$(document).on("click", ".menuItem", function(){ 
    var contentName = $(this).attr("name"); 
    $("#content > div").hide(1000, function(){ 
     $.ajax({ type: "GET", 
      url: contentName, 
      dataType: 'html', 
      success : function(responseText){ 
       $("#content").html(responseText); 
       $("#content").show(1000); 
      } 
     }); 
    }); 
}); 

但它是不工作的,因爲當調用HTML方法,新的內容出現突然。 有什麼辦法可以解決這個問題嗎?

在此先感謝!

理查德

回答

3

的這裏的問題是,$("#content")元素始終是可見的。在Ajax調用之前,您在#content - >$("#content > div")內部隱藏了一個div。

您可以在添加內容之前隱藏$("#content")

$("#content").hide().html(responseText).show(1000); 

$("#content").hide(1000, function(){ 
     $.ajax({ type: "GET", 
      url: contentName, 
      dataType: 'html', 
      success : function(responseText){ 
       $("#content").html(responseText).show(1000); 
      } 
     }); 
    }); 
+0

時間也可以傳遞到隱藏(),以進一步緩解動畫 –

+0

謝謝Corneliu,它像一個字符米:) – user1333856

0

你可以試着先隱藏內容:

$(document).on("click", ".menuItem", function(){ 
    var contentName = $(this).attr("name"); 
    $("#content > div").hide(1000, function(){ 
     $.ajax({ type: "GET", 
      url: contentName, 
      dataType: 'html', 
      success : function(responseText){ 
       $("#content").hide(); 
       $("#content").html(responseText); 
       $("#content").show(1000); 
      } 
     }); 
    }); 
}); 
3

試試這個

$(document).on("click", ".menuItem", function(){ 
    var contentName = $(this).attr("name"); 
    $("#content > div").hide(1000, function(){ 
     $.ajax({ type: "GET", 
      url: contentName, 
      dataType: 'html', 
      success : function(responseText){ 
       $("#content").hide().html(responseText).show(1000); 
      } 
     }); 
    }); 
});