Solved

Solving multiple-animation instance issue on jQuery slideToggle

Posted on 2015-01-06
4
245 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

744 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

10 Experts available now in Live!

Get 1:1 Help Now