简体   繁体   English

在同一个div中淡出淡出

[英]fadeIn fadeout in the same div

I have this code: 我有这个代码:

success:function(result) {
  $('#priceDiv').fadeOut(3000);
  $('#priceDiv').fadeIn(3000,function(){
    document.getElementById("priceDiv").innerHTML = result;
  });

In this code I want the old data from priceDiv to slowly fade out and the new data slowly fade in, in the priceDiv . 在这段代码中,我希望priceDiv的旧数据慢慢淡出,新数据在priceDiv慢慢消失。

The result holds the new data from an ajax request. 结果保存来自ajax请求的新数据。

I have already succeeded slowly fade out with old data but the fade in show the old data again and when it's finish the new data show with no effect. 我已经成功地用旧数据慢慢淡出,但淡入淡出再次显示旧数据,当它完成新数据显示没有效果。

You should use callback method of fadeOut to set data and the fadeIn 你应该使用fadeOut回调方法来设置数据和fadeIn

$('#priceDiv').fadeOut(3000, function(){
    //Set new Data
    document.getElementById("priceDiv").innerHTML = result;

    //Fade in with new data
    $('#priceDiv').fadeIn(3000);
});

Building off of what @Satpal said, try the following. 建立@Satpal所说的,尝试以下方法。

$('#priceDiv').fadeOut(3000, function(){
    var $this = $(this);
    $this.html(result);
    $this.fadeIn(3000);
});

Save off the element in a variable to prevent jQuery from having to re-traverse the DOM on each call. 保存变量中的元素以防止jQuery在每次调用时重新遍历DOM。 It's more efficient. 它效率更高。

success:function(result) {   
 $('#priceDiv').fadeOut(3000, function(){
        $(this).html(result);
        $(this).fadeIn(3000);
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM