Solved

Use jQuery to check if object is animated

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Make Float not to Wrap 15 66
How to size popup in jquery mobile 4 60
How do I allow for non standard CSV file 9 45
hide and show spans from dropdown selection 3 37
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…
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 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…

734 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