0

this is probably a very basic question, but I am trying to animate an image (with a kind of pulse effect) when a button is hovered over.

So far I have this:

$("#austriaBtn").mouseenter(
	function animate(){
		$('#austria').animate({width:"35", top:"-=4", left:"-=4"},600);
		$('#austria').animate({width:"27", top:"+=4", left:"+=4"},600, animate);
		}
	);

Now, I can hover over the button (austriaBtn) and the image (austria) starts to pulsate, however, when I take the mouse off, it carries on pulsating. How can I stop it? I know it must be something to do with stop(), but whenever I try to put it in, the animation stops working all together.

Thanks in advance!

0

3 Answers 3

1

Put .stop() in a .mouseleave() function.

$("#austriaBtn").mouseleave(function(){ $('#austria').stop(true,true); });

The true arguments in .stop() are as follows:

  • clear animation queue (default is false; shouldn't be an issue.)
  • jump to end of animation (default is false; stops the element from being caught in the middle of an animation. important here.)
Sign up to request clarification or add additional context in comments.

4 Comments

@Exinferis So I have tried adding the .stop() inside a .mouseleave() as above. However, it is resulting in some very strange and erratic behaviour. When I take the mouse off of the button, the image starts to drift slowly in a diagonal direction, which makes me think that it's not being stopped completely and that the top:"+=4", left:"+=4" are still being executed.
Just tried knocking up a jsfiddle but it worked in that o_O Will try to show it in some other way, but it might take a while. Thanks for your help so far, I really appreciate it!
@Exinferis Okay, I just tried making another JS fiddle... and yes, it's showing the strange behaviour. i.e. when you mouse on/off quickly, it gets stuck, starts drifting etc. jsfiddle.net/v88j9dx7
I still can't seem to figure this one out. I think it's getting stuck in an infinite loop sometimes, or not resetting the values. Any idea why it's doing that?
0

You have to use the corresponding mouseleave event, thus catching the event when the mouse leaves the button and then stop the animation.

$("#austriaBtn").mouseleave(
  function() {
    $("#austria").stop();
  }
);

Comments

0

you could use a lock variable:

var stopAnimation = false;
$("#austriaBtn").mouseenter(
    function animate(){
        if(stopAnimation){
            stopAnimation = false;
        } else {
            $('#austria').animate({width:"35", top:"-=4", left:"-=4"},600);
            $('#austria').animate({width:"27", top:"+=4", left:"+=4"},600, animate);
        }
    }
);
$("#austriaBtn").mouseleave(
    function(){
        $('#austria').stop();
        stopAnimation = true;
    }
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.