Solved

Use jQuery to check if object is animated

Posted on 2013-06-20
2
361 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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 If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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)
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…

911 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

17 Experts available now in Live!

Get 1:1 Help Now