2

我有一个三列布局,每一列都有多个包含相似内容块的 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 最终出现在#col2and/or#col3中,反之亦然。我正在使用这里function reorder()找到的c​​odeparadox (我删除了“撤消”功能)并且改组正在工作,但是 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>

有什么建议么?

4

2 回答 2

1

其次,你的问题对我来说确实是一个挑战。但我想我找到解决方案:

var columnsCollection = new Array();
var columnsObjectsNumber = new Array();

reorder();
function reorder() {
    $(".info-blocks-container").find("DIV[id^='col']").each(function()
    {           
        var tmp = $(this).children();
        //Store the numbers of contained children
        columnsObjectsNumber.push($(tmp).size());
        $(tmp).each(function()
        {
            //transfers current child object to our global store place
            columnsCollection.push($(this));
            //and removed it
            $(this).remove();
        });
    });

    var restartIndex = 0;
    $(".info-blocks-container").find("DIV[id^='col']").each(function()
    {
        //Contains the random generated number
        var x;
        //Contains the current object with Id 'col..'
        var temp = $(this);
        for (var i = 0; i < columnsObjectsNumber[restartIndex]; i++)
        {
            /*
              Generates a random number.
             The maximum limit is the current length from our global store place
            */
            x = Math.floor(Math.random() * columnsCollection.length);
            //Appends the object to the current object with Id 'col..'
            $(temp).append(columnsCollection[x]);
            //Removes the appended element from global store place
            columnsCollection.splice(x,1);
         }
        restartIndex++;
    });   
}

它看起来并不专业,但它确实有效。希望这是您要求的...

于 2012-09-11T15:12:07.297 回答
1

起初,您不需要看起来相似的三个功能。您可以只用一种方法进行转换:

reorder();
function reorder(columnObject) {
    $(".info-blocks-container").find("DIV[id^='col']").each(function()
    {
        var grp = $(this).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();
         $(this).append($(grp));  
    });
}
于 2012-09-11T14:58:02.240 回答