ajax帖子在成功后逐渐淡出

ajax post fade in after success

本文关键字:淡出 成功 ajax      更新时间:2023-09-26

这可能是一个简单的问题,但我已经尝试了几个小时。我在stackoverflow上几乎试过了所有我发现的东西,但似乎都不起作用。也许一双新鲜的眼睛可以告诉我如何做到这一点。

我想在调用这个javascript函数后淡入div。

            function getRequest(){
            $.ajax({
                type : "POST",
                url : "/includes/requests.php",
                data : "mode=get_requests",
                success : function(result)  { $("#requests").html(result);
                    $('#requests').fadeIn(9000);
                }
            });
        }

编辑:谢谢大家。这个

$('#requests').hide().fadeIn(9000);

做到了。

$('#requests').hide().fadeIn(9000);

您需要先隐藏元素,然后才能显示它。因此.hide()然后是.fadeIn()

$('#requests').hide();
function getRequest(){
    $.ajax({
        type : "POST",
        url : "/includes/requests.php",
        data : "mode=get_requests",
        success : function(result)  {
            $("#requests").html(result);
        }
    }).fail(function() {
        $('#requests').html('The AJAX failed!!');
    }).always(function() {
        $('#requests').fadeIn(9000);
    });
}

通过将.fadeIn()方法放在.always()回调中,您可以获得功能,因为您将知道AJAX是否成功。

您需要在淡入之前隐藏元素,因为它对用户来说已经是可见的,如下所示:

$('#requests').hide().fadeIn(9000);