Solved

Use jQuery to check if object is animated

Posted on 2013-06-20
2
362 Views
Last Modified: 2013-06-20
This is my page:
http://sterlingmodular.com/test/plan-series.htm

The arrow graphic on the left side of the row of images near the bottom... if you click it, there is a brief animation to indicate you cannot go any further to the left. If you click it twice very quickly, it will misfire because an animation is already in progress.

I tried using .not(':animated') on the object like this:

$('#arrowleft').click(function(){
	if ($('#imgPlanSeriesContainer').not(':animated')){
		if ($('#imgPlanSeriesContainer').position().left===12){
			$('#imgPlanSeriesContainer').animate({left:'+=20px'}, 200);
			$('#imgPlanSeriesContainer').animate({left:'-=20px'}, 200);
		} else {
			$('#imgPlanSeriesContainer').animate({left:'+=244px'});
		}
	} else {
		console.log('animated');
	}
});

Open in new window


However, it doesn't seem to pick it up. It will allow you to click twice very quickly and still cause it to misfire.

I basically want to say, "if the element is currently being animated, don't do anything". Is there something wrong with my code? I don't get any error messages.

Thank you!
0
Comment
Question by:bbdesign
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39262291
Add a class just before animate and remove it when animation is done :
if ($('#imgPlanSeriesContainer')[b].hasClass('animated')[/b]){
   $('#imgPlanSeriesContainer').[b]addClass("animated")[/b];
   $('#imgPlanSeriesContainer').animate({left:'+=20px'}, 200, function() {
        $('#imgPlanSeriesContainer').[b]removeClass("animated")[/b];    
  });

Open in new window

0
 

Author Comment

by:bbdesign
ID: 39262315
Ah, very clever. This is a technique I think I can reuse often. Thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

776 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