tags:

views:

112

answers:

3

I have a function that runs on document load:

    function animateText() {
        $('.block')
        .css("background-image", "url(images/text-4-line-1.png)")
        .animate({left: "526"}, 1, "linear" ) .delay(2200).fadeIn(1000)
        .delay( 3000)
        .fadeOut(1000)
        $('.block2')
        .css("background-image", "url(images/text-4-line-2.png)")   
            .animate({left: "484"}, 1).delay(3200).fadeIn(1000)
         .delay( 2500)
          //etc...
    }

I want to have a button that will stop this function from running and return all elements to original positions. I also want to have another button that will replay the animation function.

I can't get this to work.

Any help would be great!!

Thanks!

A: 
.stop(true, true)

will stop the animations and clear the jQuery fx queue.

To "replay" your animaten, it should be just fine to call your animateText() function again.

$('#yourbutton').click(animateText);

Reference: .stop()

jAndy
`.stop()` alone won't return all elements to the original state, will it?? and I think `.stop(true, true)` will make the elements go to its final destination... :)
Reigel
@Reigel: calling `stop()` with true on both parameters will indeed jump to the final state, which is probably what the OP wants. I didn't think its worth to mention that you have to reset an element before modifying it again, but maybe I'm wrong there.
jAndy
hehe, I might just also confused on this statement from the OP, `return all elements to original positions.`
Reigel
A: 

.stop will be helpful in your case.

Krunal
`.stop()` alone won't return all elements to the original state, will it??
Reigel
@Reigel - Agree with you and jAndy.
Krunal
+1  A: 

if your styling your dom elements not inline (e.g. style="color: red;"), this might do it...

$('#stop').click(function(){
   $('.block,.block2').stop().removeAttr('style');
});
$('#play').click(function(){
   $('.block,.block2').stop(true,true).removeAttr('style');
   animateText();
});

note: don't do inline styling...

I made this fun demo

Reigel
Thanks for your quick response, this works to send the animation back to the start, however all of the timing is lost. Im relying on .delay pretty heavily to get the timing right! Any other suggestions would be greatly appreciated.Thank you!!
Melzee
I don't think it will affect the delay... can you explain the situation?... like when does the delay was lost...
Reigel
Thanks for your help! I worked out my problem with your suggestion.YAY!!
Melzee
just don't forget to accept the answer by clicking the check on the left of my answer :)
Reigel
Hi,Not quite there yet. Have a look at: http://flytype.com/108/The animation that runs in the background is what I want to stop / send back to start with buttons. I have followed your suggestion (Reigel), but the results are quite what I was after.If you have the time, please take a look. Im grateful for any suggestions!Thanks.
Melzee
aha! find this line `('.block','.block2','#bg1','#bg2').stop(true,true).removeAttr('style');` you missed a `$`, you should have look at it at firebug debugger.. ;)
Reigel