使用jquery随机播放一些div

时间:2013-03-15 17:08:09

标签: javascript jquery html

我想要洗牌div表的单元格。我尝试使用.shuffle()插件,但无法使其正常工作。这是我的代码:

<div class="sprites">
    <div class="row">
        <span id="draggable0" class="draggable"></span> 
        <span id="draggable1" class="draggable"></span> 
        <span id="draggable2" class="draggable"></span>
    </div>
    <div class="row">
        <span id="draggable3" class="draggable"></span> 
        <span id="draggable4" class="draggable"></span> 
        <span id="draggable5" class="draggable"></span>
    </div>
    <div class="row">
        <span id="draggable6" class="draggable"></span> 
        <span id="draggable7" class="draggable"></span> 
        <span id="draggable8" class="draggable"></span>
    </div>
</div>

这就是我试图洗牌的方式:

$(document).ready(function () {
    $('.sprites').shuffle();
});   

你知道我怎么能把这些东西洗牌吗?

2 个答案:

答案 0 :(得分:5)

Fiddle

这只会改变班级sprites的元素。您想要在div内对.sprites进行随机播放,对吧?

$('.sprites div').shuffle();

编辑:正确的语法实际上可能是:

$.shuffle('.sprites div');

答案 1 :(得分:0)

shuffle()随机播放传递给它的元素......你需要将盒子里的物品传递给它......

只需使用:Live Demo

$('.draggable').shuffle();

$('.row').shuffle();

更新

使用此选项重新启用拖动。

$(document).ready(function() {
       $('.draggable').draggable(); 
});

function shuffle () {
    $('.draggable').draggable("destroy");
    $('.draggable').shuffle();
    $('.draggable').draggable();
}

随机播放代码

(function(d){
    d.fn.shuffle=function(c){
        c=[];
        return this.each(function()
            {
                c.push(d(this).clone(true))
            }).each(function(a,b)
            {
                d(b).replaceWith(c[a=Math.floor(Math.random()*c.length)]);
                c.splice(a,1)
            })
    };
    d.shuffle=function(a){
        return d(a).shuffle()
    }
})(jQuery);