Pedro Chagas
asked on
jQuery, Javascript, stop the execution of a animation / function
Hi E's. I have a script where will run a animation. The animation start automatic after load the page. The code have a "div stop" that if I click, the function demo() will stop execution (In my example the animation is approximately 10 seconds long (but can be much more time, and that is the reason for can stop the animation in the middle of the execution).
Here is my code:
The best regards, JC
Here is my code:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<style type="text/css">
#geral{position: absolute; width: 600px; height: 600px;}
#piece{position: absolute; width: 13px; height: 20px; left: 20px; top: 150px;}
/* the new element (for show after the first stage):*/
#square{position: absolute; width: 20px; height: 20px; background-color: red; display:none; left: 500px; top: 500px;}
</style>
</head>
<body>
<div id="geral">
<div id="piece"><img src="ponteiro_rato.png" alt="Rato" width="13" height="20" /></div>
<div id="square"></div>
<div id="stop">stop demonstration</div>
</div>
<script>
demo();
function demo(){
$('#piece').animate({ left: '500', top: '500' }, 2500, function(){
$('#square').show();
var t = setTimeout(function(){
$('#square').hide();
$('#piece').animate({ left: '150', top: '100' }, 2500);
},5000);
});
}
$("#stop").click(function(){
//DO SOMETHING HERE TO STOP THE EXECUTION OF THE FUNCTION demo()
});
</script>
</body>
</html>
</body>
</html>
What I have to write in line 34 for stop the execution of function demo()?The best regards, JC
ASKER
Hi, thanks for the answer. Just a clarification, in every situations will always use "stop"?
My doubt is about if have lots of "divs" in animation in simultaneous, I have to do "stop" for all. Maybe have another solution for stop all.
Can you give me a clarification?
~JC
My doubt is about if have lots of "divs" in animation in simultaneous, I have to do "stop" for all. Maybe have another solution for stop all.
Can you give me a clarification?
~JC
use the same class for all your div your want to stop so instead using an ID :
$("#ID_OF_THE_DIV").stop() ;
use :
$(".CLASS_OF_THE_DIVS").st op();
bad design but work too :
$("#ID_OF_THE_DIV1,#ID_OF_ THE_DIV3,# ID_OF_THE_ DIV2").sto p();
$("#ID_OF_THE_DIV").stop()
use :
$(".CLASS_OF_THE_DIVS").st
bad design but work too :
$("#ID_OF_THE_DIV1,#ID_OF_
ASKER
Hi @leakim971 I did some change to my code, based in your last post:
The code in snippet code works well, but when I change the line 34 for "$(".anim").stop();" don't work. It was supposed to stop all elements with the class "anim"?
~JC
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<style type="text/css">
#geral{position: absolute; width: 600px; height: 600px;}
#piece{position: absolute; width: 13px; height: 20px; left: 20px; top: 150px;}
/* the new element (for show after the first stage):*/
#square{position: absolute; width: 20px; height: 20px; background-color: red; display:none; left: 500px; top: 500px;}
</style>
</head>
<body>
<div id="geral">
<div id="piece"><a class="anim"><img src="ponteiro_rato.png" alt="Rato" width="13" height="20" /></a></div>
<div id="square"><a class="anim"></a></div>
<div id="stop">stop demonstration</div>
</div>
<script>
demo();
function demo(){
$('#piece').animate({ left: '500', top: '500' }, 2500, function(){
$('#square').show();
var t = setTimeout(function(){
$('#square').hide();
$('#piece').animate({ left: '150', top: '100' }, 2500);
},5000);
});
}
$("#stop").click(function(){
$("#piece").stop();
});
</script>
</body>
</html>
</body>
</html>
I associate the class "anim" to the divs #square and #peace.The code in snippet code works well, but when I change the line 34 for "$(".anim").stop();" don't work. It was supposed to stop all elements with the class "anim"?
~JC
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok, I forget the class one.
You give me this solution, not yet tested ($("#ID_OF_THE_DIV1,#ID_OF _THE_DIV3, #ID_OF_THE _DIV2"), and I wonder:
Is possible do something like in that line:
$("#div*"); where the * is a div1 to div9?
~JC
You give me this solution, not yet tested ($("#ID_OF_THE_DIV1,#ID_OF
Is possible do something like in that line:
$("#div*"); where the * is a div1 to div9?
~JC
wait, you have simpler, just use :
$("#stop").click(function(){
$.fx.off = true;
});
ASKER
Don't work, but sounds good.
~JC
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<style type="text/css">
#geral{position: absolute; width: 600px; height: 600px;}
#piece{position: absolute; width: 13px; height: 20px; left: 20px; top: 150px;}
/* the new element (for show after the first stage):*/
#square{position: absolute; width: 20px; height: 20px; background-color: red; display:none; left: 500px; top: 500px;}
</style>
</head>
<body>
<div id="geral">
<div id="piece"><a class="anim"><img src="ponteiro_rato.png" alt="Rato" width="13" height="20" /></a></div>
<div id="square"><a class="anim"></a></div>
<div id="stop">stop demonstration</div>
</div>
<script>
demo();
function demo(){
$('#piece').animate({ left: '500', top: '500' }, 2500, function(){
$('#square').show();
var t = setTimeout(function(){
$('#square').hide();
$('#piece').animate({ left: '150', top: '100' }, 2500);
},5000);
});
}
$("#stop").click(function(){
$.fx.off = true;
});
</script>
</body>
</html>
</body>
</html>
~JC
oh yes, it's to disable or not animation, this is not what you want
so use the previous one : $("#ID_OF_THE_DIV1,#ID_OF_ THE_DIV3,# ID_OF_THE_ DIV2")
if you want to follow your id $("#div*"); here the good syntax : $("[id^=div]").stop();
so use the previous one : $("#ID_OF_THE_DIV1,#ID_OF_
if you want to follow your id $("#div*"); here the good syntax : $("[id^=div]").stop();
So :
$('#piece').stop();
or :
$('#piece').stop(true, true);
Test page : http://jsfiddle.net/HyPFd/