I have an unordered list. When i click a shuffle button, i need:
- the items to fade out
- then randomise
- then fade back in.
All separate parts of my script work. The animated fade works on it’s own, and the shuffle works. The problem is when shuffle is in the script, that it skips straight to the shuffle, and stops all fade in or out.
Here is my script:
$('.vShuffle').click(function(){
$('.list li').fadeOut(1000).shuffle().fadeIn(1000);
});
I have also tried this, which will fade out, then i assume it shuffles because it doesn’t do anything else after:
$('.vShuffle').click(function(){
$('.list li').fadeOut(1000, function() {
$('.list li').shuffle();
$('.list li').fadeIn(1000);
});
});
The shuffle script i’m using is from here, it works fine other than in my chain: http://mktgdept.com/jquery-shuffle
(function(d){d.fn.shuffle=function(c){c=[];return this.each(function(){c.push(d(this).clone(true))}).each(function(a,b){d(b).replaceWith(c[a=Math.floor(Math.random()*c.length)]);c.splice(a,1)})};d.shuffle=function(a){return d(a).shuffle()}})(jQuery);
Any help would be greatly apprecitated.
Thanks
Without doing some substantial debugging I can’t know exactly what’s going wrong, but I’m guessing the problem has something to do with the following:
Do you realize that the code inside the
fadeOutcallback (i.e. theshuffle()andfadeIn()function calls) is getting run once per every.list lielement? In other words, if you have 10 elements that match the.list liselector, you’re actually shuffling and fading in all the.list lielements 10 times. That’s because.fadeOutwill be called on every single matching.list lielement, and therefore its callback will then be called.Instead, just call
.fadeOuton the containingulelement, and then in the callback call.fadeInon that containing element as well:Here’s a working example: http://jsfiddle.net/mdur4/