Problem with jquery animation

Tags: jQuery, JavaScript

I always use jquery to animate the DOM in my design. but the problem is when user wants to see if website is working perfectly or not, for example when I have a scroller or accordion or some tools with a nice animation on the page.

So this problem is not special for me, but some time I face it.
OK, go to this address and http://flowplayer.org/tools/index.html and press Right then Left arrow key on your keyboard and then repeat it too many times quickly* and leave it to do all the animation in queue, you can see the problem, the animation is repeated on and on

So where is the problem?
take a look to this sample code of jQuery Tools main page:

// initialize scrollable and return the programming API
var api = $("#scroll").scrollable({
	items: '#tools'

// use the navigator plugin
}).navigator().data("scrollable");


// this callback does the special handling of our "intro page"
api.onBeforeSeek(function(e, i) {

	// when on the first item: hide the intro
	if (i) {
		$("#intro").fadeOut("slow");

		// dirty hack for IE7-. cannot explain
		if ($.browser.msie && $.browser.version < 8) {
			$("#intro").hide();
		}

	// otherwise show the intro
	} else {
		$("#intro").fadeIn(1000);
	}

	// toggle activity for the intro thumbnail
	$("#t0").toggleClass("active", i == 0);
});

// a dedicated click event for the intro thumbnail
$("#t0").click(function() {

	// seek to the beginning (the hidden first item)
	$("#scroll").scrollable().begin();

});
In onBeforeSeek event Method you can see the fadeIn and fadeOut, so they add the new animation effect into the queue and never clear them.
So you can see another problem here; when the animation begins and you press the Left arrow key immediately, you have to wait for it to be complete until the next animation begins.
We have 2 problems here:
1. Waiting for the current animation to be complete.
2. The queue list
Solution to fix these problems:
We must stop the animation and then clear the queue list.
$.fn.stop() // Stop the current animation.
$.fn.clearQueue() // Clear the all job(s) in queue list
OK we need to change fadeOut and fadeIn with the code below:
$("#intro").stop().clearQueue().fadeTo("slow", 0, function () { $(this).hide(); });
.
.
.
$("#intro").stop().clearQueue().fadeTo(1000, 1).show()

I don't have a good feeling about this, we must write it all the time and it is not good mostly when we are working on animating page, maybe is better to make a new fn function to do it:

(function ($) {
    $.fn.extend({
        c: function () {
            return this.each(function () { $(this).stop().clearQueue() });
        }
    })
})(jQuery);

Now it is more better, now we can use .c() instead of .stop().clearQueue() . for example see the code below:

$("#intro").c().fadeTo("slow", 0, function () { $(this).hide(); });
.
.
.
$("#intro").c().fadeTo(1000, 1).show()

Test it again, press Right and then Left key 1000 times, it never happens again and it will be stopped and roll backed to previews page in middle of animating.
I hope you enjoy reading this.

1 Comment

Add a Comment

Follow me

  • rss