最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery animate not firing callback function - Stack Overflow

programmeradmin4浏览0评论

I have the following jQuery animate function:

$myDiv.animate({ "left": "0%" }, { duration: 1000, easing: 'easeInOutExpo' }, 
    function () {
        alert('hi');
    }
);

The animation itself works. $myDiv slides with the easeInOutExpo effect, as desired. However, the callback function is never fired. To test it, I changed the callback to just alert("hi");, as you can see above. Still doesn't work.

What could I be doing wrong?

I have the following jQuery animate function:

$myDiv.animate({ "left": "0%" }, { duration: 1000, easing: 'easeInOutExpo' }, 
    function () {
        alert('hi');
    }
);

The animation itself works. $myDiv slides with the easeInOutExpo effect, as desired. However, the callback function is never fired. To test it, I changed the callback to just alert("hi");, as you can see above. Still doesn't work.

What could I be doing wrong?

Share Improve this question edited Jul 14, 2014 at 0:55 royhowie 11.2k14 gold badges53 silver badges67 bronze badges asked Jul 14, 2014 at 0:21 NullqwertyNullqwerty 1,1681 gold badge21 silver badges38 bronze badges 2
  • Unless you've set $myDiv to equal $("#myDiv"), it should be the latter. – AstroCB Commented Jul 14, 2014 at 0:25
  • @AstroCB Yeah, $myDiv is equal to that. It's dynamic and changes which is why it's a variable. It is properly set and the animation does work. Just not the plete function. – Nullqwerty Commented Jul 14, 2014 at 0:36
Add a ment  | 

3 Answers 3

Reset to default 4

Try this

Demo: jsFiddle

  $("#myDiv").animate({ "left": "0%" }, { duration: 1000, easing: 'easeInOutExpo' , 
    plete:function () {
        alert('hi');
    }
   }
);

There are a couple of things that need fixing here:

  1. Make sure you've included jQuery UI in your code, because easeInOutExpo is not part of the standard jQuery library.
  2. Your syntax is wrong: you're mixing up the two different options for the animate() function.

It's either

$(element).animate(properties [,duration] [,easing] [,plete]);

or

$(element).animate(properties, options)

where options is an object formatted like this:

{
  duration: number,
  easing: string,
  plete: function,
}

You've gone with the second option, so you need to format it properly to use the plete attribute of the options object for your function:

$myDiv.animate({
    "left": "0%",
}, {
    duration: 1000,
    easing: "easeInOutExpo",
    plete: function () {
        alert('hi');
    },
});

Demo

Alternatively, you could use the first format option:

$("#myDiv").animate({
    "left": "0%",
}, 1000, "easeInOutExpo", function () {
    alert('hi');
});

Demo

One way is use of JQuery Promise

$myDiv.animate({ "left": "0%" }, { duration: 1000, easing: 'easeInOutExpo' }).promise().done(function(){

     alert('hi done');

});
发布评论

评论列表(0)

  1. 暂无评论