如何使用jQuery以随机顺序显示图像

Ric*_*uez 2 jquery

当我有这个代码时:

$("img").each(function(i) {
   $(this).delay(i*100).fadeIn('slow');
});
Run Code Online (Sandbox Code Playgroud)

可以看到在这里居住,以更好地举例说明它的作用......

...如何使这些图像以随机顺序出现?这意味着不是从左上角到右下角,而是随机像图像45,图像100,图像3等......?

dru*_*dge 6

http://blog.staydecent.ca/entry/jquery-random-each

function randsort(c) {
    var o = new Array();
    for (var i = 0; i < c; i++) {
        var n = Math.floor(Math.random()*c);
        if( jQuery.inArray(n, o) > 0 ) --i;
        else o.push(n);
    }
    return o;
}

var e = $('div'); // The elements we're searching
var c = e.size(); // Total number of those elements
var r = randsort(c); // an array of the element indices in random order

$("div").each(function(i) {
    var e = $(this);
    e.fadeTo(0, 0.05);
    setTimeout(function(){
        e.fadeTo(250, 1);
    }, r[i]*10);
});
Run Code Online (Sandbox Code Playgroud)