Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Solving multiple-animation instance issue on jQuery slideToggle

Posted on 2015-01-06
4
Medium Priority
?
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

618 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