jQuery animate()运行延迟

jQuery animate() is running late

本文关键字:运行 延迟 animate jQuery      更新时间:2023-09-26

我有一个脚本,它在标题上为我的图像设置了动画。当用户向下滚动时缩小,当用户到达顶部时放大。这是:

function headerScroll() {
    $(window).scroll(function() {
        if($(document).scrollTop() != 0) {
            $('#headerContainer').addClass('smaller');
            $('#header img').animate({
                height: '170px',
                left: '414px',
                top: '10px'
            },200);
        } else if($(document).scrollTop() < 100) {
            $('#header img').animate({
                height: '307px',
                left: '361px',
                top: 0
            },200, function() {
                $('#headerContainer').removeClass('smaller');
            });
        }
    });
}

问题是,有时(并非总是)秒动画会在一段时间内不运行。它有时要等几秒钟。我该如何预防?

edit:当我在animate之前或之后添加alert('test')时,它运行得很好。

使用:$('#header img').animate().stop().animate(
希望是工作