【发布时间】:2015-09-03 16:35:10
【问题描述】:
我正在尝试随机化我制作的这个函数,但我只找到了一个随机 TimeOut 解决方案。我希望顺序是随机的,而不是超时。
$(document).ready(function() {
function boucle() {
$('#test img').fadeTo(1000, 0);
$('#test img').delay(1000).each(function(i) {
$(this).delay((i++) * 1500).fadeTo(1500, 1);
});
}
boucle();
setInterval(boucle, 15000);
});
我试过this solution 但这不是我想要的。而this one 对我不起作用。
有人可以帮我做那个随机循环吗?提前谢谢你。
编辑:
这是我如何实现@jack 提供的解决方案:
(function($) {
$.fn.shuffle = function() {
// credits: http://bost.ocks.org/mike/shuffle/
var m = this.length, t, i;
while (m) {
i = Math.floor(Math.random() * m--);
t = this[m];
this[m] = this[i];
this[i] = t;
}
return this;
};
}(jQuery));
function boucle() {
$('#test img').fadeTo(1000, 0);
$('#test img').shuffle().delay(1000).each(function(i) {
$(this).delay((i++) * 1500).fadeTo(1500, 1);
});
}
boucle();
setInterval(boucle, 10000);
【问题讨论】:
-
您的意思是要随机化
each()遍历元素的顺序? -
@RoryMcCrossan 完全正确!