Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do something in JQuery after animation Finish()

I have this function to move an absolute DIV and I want to execute the setTimeout function. However, JQuery jumps out of the hover() function when it comes to the line $().finish(). How do I execute something after the finish()?

$('#header li[class!="logo"]').hover(function () {

    var leftStart = $(this).position().left;
    var width = ($(this).width() / 2) - 22;

    $('#header .pointerarrow').animate({ left: leftStart + width }, 400);

}, function () {
    $('#header .pointerarrow').finish();

    //######This does not excecute###########
    setTimeout(function () {
        alert('succeeded');
        var l = $('#header li[class="current"]').position().left;
        var b = ($('#header li[class="current"]').width() / 2) - 22;
        $('#header .pointerarrow').css({ left: l + b });
    }, 500);

});
like image 489
Jelle Avatar asked May 09 '14 08:05

Jelle


People also ask

How can use a animate () method in jQuery?

The animate() method performs a custom animation of a set of CSS properties. This method changes an element from one state to another with CSS styles. The CSS property value is changed gradually, to create an animated effect. Only numeric values can be animated (like "margin:30px").

What is the correct syntax to call the jQuery animate () function?

jQuery Animations - The animate() Method The jQuery animate() method is used to create custom animations. Syntax: $(selector). animate({params},speed,callback);

Which jQuery method is used to stop an animation before it is finished?

The jQuery stop() method is used to stop an animation or effect before it is finished. The stop() method works for all jQuery effect functions, including sliding, fading and custom animations. Syntax: $(selector).

What is finish method in jQuery?

jQuery finish() Method The finish() method stops the currently-running animations, removes all queued animations, and completes all animations for the selected elements. This method is similar to the . stop(true,true) method, except that finish() also causes the CSS property of all queued animations to stop.


2 Answers

$('#header .pointerarrow').animate(
    { left: linksstart + breedte },
     400, function() {
       // Animation complete.
  });

What ever you want to perform after complete animation write inside function block.

like image 105
dgk Avatar answered Sep 22 '22 15:09

dgk


Try this:

$('#header .pointerarrow').animate({ left: linksstart + breedte }, 400);
$('#header .pointerarrow').promise().done(function(){
    /* PUT FUNCTION HERE */
});

I hope this helps!

like image 45
KM123 Avatar answered Sep 23 '22 15:09

KM123