Solved

Use jQuery to check if object is animated

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
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…

708 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

18 Experts available now in Live!

Get 1:1 Help Now