我有一个三列布局,每一列都有多个包含相似内容块的 div:
<section class="info-blocks-container clearfix">
<div id="col1">
<div class="info-block">...</div>
<div class="info-block">...</div>
<div class="info-block">...</div>
</div>
<div id="col2">
<div class="info-block">...</div>
<div class="info-block">...</div>
</div>
<div id="col3">
<div class="info-block">...</div>
<div class="info-block">...</div>
<div class="info-block">...</div>
</div>
</section>
我想随机打乱div#col1
, div#col2
, 和div#col3
列中#col1
的子 div ,所以一些子 div 最终出现在#col2
and/or#col3
中,反之亦然。我正在使用这里function reorder()
找到的codeparadox (我删除了“撤消”功能)并且改组正在工作,但是
1.我有3个单独的重复函数的方式非常脏,
2.它只是对每一列中的div进行排序(它是不在三列之间共享子 div):
<script type="text/javascript">
$(function(){
reorder();
reorder2();
reorder3();
function reorder() {
var grp = $(".info-blocks-container > div:nth-child(1)").children();
var cnt = grp.length;
var temp, x;
for (var i = 0; i < cnt; i++) {
temp = grp[i];
x = Math.floor(Math.random() * cnt);
grp[i] = grp[x];
grp[x] = temp;
}
$(grp).remove();
$(".info-blocks-container > div:nth-child(1)").append($(grp));
}
function reorder2() {
var grp = $(".info-blocks-container > div:nth-child(2)").children();
var cnt = grp.length;
var temp, x;
for (var i = 0; i < cnt; i++) {
temp = grp[i];
x = Math.floor(Math.random() * cnt);
grp[i] = grp[x];
grp[x] = temp;
}
$(grp).remove();
$(".info-blocks-container > div:nth-child(2)").append($(grp));
}
function reorder3() {
var grp = $(".info-blocks-container > div:nth-child(3)").children();
var cnt = grp.length;
var temp, x;
for (var i = 0; i < cnt; i++) {
temp = grp[i];
x = Math.floor(Math.random() * cnt);
grp[i] = grp[x];
grp[x] = temp;
}
$(grp).remove();
$(".info-blocks-container > div:nth-child(3)").append($(grp));
}
});
</script>
有什么建议么?