Solved

Solving multiple-animation instance issue on jQuery slideToggle

Posted on 2015-01-06
4
250 Views
Last Modified: 2015-01-07
Here is my test website:
http://beta.stoneridgeretirement.com

The drop-down navigation menu at the top is handled with this function:

$('ul#nav1 li').on('mouseenter mouseleave', function(){
	$(this).children('ul').slideToggle(100);
});

Open in new window


In general, it works great, and is obviously very simple. However, it is possible to make it malfunction by moving your mouse very quickly from the main (blue) button/link, downward to where the drop-down menu appears. If you do it "just right", the drop-down menu will repeatedly slide up and down, a whole bunch of times, before finally stopping.

I have handled this kind of thing in the past using a variety of methods, most often by setting a variable that says "animation is on" and setting it back to "animation is off" after the animation completes. But I'm not sure that will work in this case, at least not using slideToggle.

My current function is so simple, I hate to make a mess of it by over complicating the code. Yet, not sure I can avoid that. I'm not even exactly sure "why" the problem exists, or what is being triggered exactly when you move the mouse in that way, etc.

Would appreciate any insight into a possible solution. This kind of thing comes up a lot with jQuery animations.

Thank you.
0
Comment
Question by:bbdesign
  • 2
  • 2
4 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 40534359
You can stop any in-progress animations before starting new ones by adding stop().

        $('ul#nav1 li').on('mouseenter mouseleave', function(){
      $(this).children('ul').stop().slideToggle(100);

When an animation that's triggered by both mouseenter and mouseleave is in the same handler you can get stuck in a loop where the mouse leave triggers the animation which in turn causes a mouse enter when the animated item moves passed the cursor, rinse and repeat to infinity.
0
 

Author Comment

by:bbdesign
ID: 40536646
Thank you, that seems to help quite a bit! I separated my slideToggle out into two separate functions, and added stop(), the problem seems to have gone away.
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 40536669
You're welcome.

I was not able to reproduce the problem in my test after adding the stop() to the single event handler. Maybe your experience was different?
0
 

Author Comment

by:bbdesign
ID: 40536714
Maybe that would work too, I just felt more "in control" by having the two functions, I guess.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now