我在哪里把jQuery .fade()函数放在ajax成功回调中?

时间:2013-04-08 15:10:24

标签: javascript ajax jquery fadein

我有一个ajax调用,它使用在成功回调中返回的html填充页面的一部分:

function LoadCurrentCourses(masterKey, status) {
status = 'S';
$.ajax({
    type: "GET",
    url: "/Home/LoadCurrentCourses/?masterKey=" + masterKey + "&status=" + status,
    dataType: "html",
    success: function (evt) {
        $('#currentCourses').fadeIn(1500, function () { $('#currentCourses').html(evt) });
    },
    error: function (req, status, error) {
        $('#currentCourses').html("<p>Error occured retrieving current courses</p>");
    }
});

}

#currentCourses标记只有一个标题并在那里加载.gif文件。

<div class="span4 moduleBox" id="currentCourses">
    <h2>Current Courses</h2>
    <img style="margin-left: 45%; margin-top: 5%; margin-bottom: 5%;" src="~/Images/11.gif" />
</div>

从doc.ready()的主页面调用ajax:

    <script>
    $(document).ready(function () {
        var masterKey = '@Model.MasterKey';

        //load degree overview
        LoadCurrentCourses(masterKey);

    });
</script>

我想要做的是返回一个数据我希望html慢慢淡入主页,替换gif和标题。现在它工作正常,一切都加载,并被替换,但我似乎无法弄清楚我应该把jQuery .fadein()方法放在哪里。

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

你应该在淡入之前加载文本

success: function (evt) {
    $('#currentCourses').html(evt);
    $('#currentCourses').fadeIn(1500);
},

您可能还需要在淡入之前淡出或隐藏该容器,否则它似乎什么也不做,只是加载新数据。

答案 1 :(得分:2)

应该像:

success: function (evt) {
    $('#currentCourses').html( evt ).fadeIn(1500);
}

在元素填充后淡入,否则在.html()回调中调用.fadeIn()将导致:

fade--(fade ends)-->--(HTML applied)