jQuery动画回调不起作用

时间:2011-12-13 03:20:20

标签: javascript jquery animation callback

为什么不发出警报?

var $anchor = $(this);

$('.hide').val($(this).attr('href'));
$('html, body').animate({
    scrollLeft: $($anchor.attr('href')).offset().left
}, {
    queue: false,
    duration: 1000,
    easing: 'easeInOutCirc'
}, function () {
    alert('test');
});

2 个答案:

答案 0 :(得分:9)

.animate()可以使用多种不同的语法选项。当你传递一个属性对象和一个options对象(就像你正在做的那样)时,completion函数会在options对象中输入,而不是像这样的第三个参数:

var $anchor = $(this);

$('.hide').val($(this).attr('href'));
$('html, body').animate({
    scrollLeft: $($anchor.attr('href')).offset().left
  }, {
    queue: false,
    duration: 1000,
    easing: 'easeInOutCirc',
    complete: function () {
        alert('test');
    }
  }
);

jQuery .animate() doc中已完整描述了这一点。

.animate( properties, options )

properties - A map of CSS properties that the animation will move toward.

options - A map of additional options to pass to the method. Supported keys:
    duration: A string or number determining how long the animation will run.
    easing: A string indicating which easing function to use for the transition.
    complete: A function to call once the animation is complete.
    step: A function to be called after each step of the animation.
    queue: A Boolean indicating whether to place the animation in the effects queue. If false, the animation will begin immediately. As of jQuery 1.7, the queue option can also accept a string, in which case the animation is added to the queue represented by that string.
    specialEasing: A map of one or more of the CSS properties defined by the properties argument and their corresponding easing functions (added 1.4).

答案 1 :(得分:2)

尝试将第三个参数指定为“完整”,如下所示:

var $anchor = $(this);

$('.hide').val($(this).attr('href'));
$('html, body').animate({
    scrollLeft: $($anchor.attr('href')).offset().left
}, {
    queue: false,
    duration: 1000,
    easing: 'easeInOutCirc'
}, complete: function () {
    alert('test');
});