4

I would like to loop through a collection of divs and randomly fade them out when a click event is triggered but at the moment I have to continually click to fade the other divs all out. I would rather click a div and have all its divs randomly fade out. I have added some console.logs into the while loop and everything seems to work fine, problem is when I try to fadeout the actual elements. If anyone could help that would be great?

Fiddle here: http://jsfiddle.net/kyllle/sdpzJ/7/

3
  • 2
    Please post some relevant code here and please don't sign your posts Commented Jan 31, 2012 at 14:59
  • Hi, link to fiddle in post and signature removed :) Commented Jan 31, 2012 at 15:02
  • 1
    Great. Now if you can post the relevant code here on SO we may be able to help you with your issue. Commented Jan 31, 2012 at 15:05

4 Answers 4

5

I'm not sure if I understand your question, but here's a possible solution:

function randomFadeOut(i){  
    var random;
    var e = 0;
    while (e < ctnLength) { 
        random = Math.random() * 1000;
        $(ctn[e]).not(i).delay(random).animate({ opacity : 0 });
        e++;
    }        
}

This will fade out all the divs at random times when you click on one.

I updated your fiddle here.

Sign up to request clarification or add additional context in comments.

Comments

2

Your random number generator is outside of your loop - so you only get one random number over and over.

Try this:

 function randomFadeOut(i){  
        var random      
        for (var e=0;e<ctnLength;e++) {
            random = Math.floor(Math.random() * ctnLength);
            $(ctn[random]).not(i).animate({ opacity : 0 });
        }        
    }

Of course, since this is random, the same cells can be selected more than once, which will leave a number of cells behind.

Comments

1

Decided to throw this out there, too. Simplified.

$(function() {
    var $ctn = $('#container .ctn');

    function randomFadeOut() {
        var $r = $ctn.not($(this));
        var e = 0;
        while (e < $ctn.length) {
            $r.eq(e).delay(Math.random() * 500).animate({ opacity: 0 });
            e++;
        }
    }

    $ctn.hide().click(randomFadeOut).each(function(v) {
        $(this).delay(50 * v).fadeIn();
    });
});

http://jsfiddle.net/sdpzJ/15/

Comments

1

Here is a better and more efficient randomFade function:

function randomFadeOut(i){          
    var tmp = ctn.toArray();
    tmp.sort( function(){ return Math.floor( Math.random()*3 ) -1; } );
    for( var i=0; i<tmp.length; ++i ){
        $(tmp[i]).delay(100 * i).fadeOut();
    }
}

This way, you only go once through the array I updated your fiddle with it as well to see it in action :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.