【问题标题】:AddClass to random order of list items untill all have class added to them将类别添加到列表项的随机顺序,直到所有类别都添加到它们
【发布时间】:2014-10-14 03:51:58
【问题描述】:

这是一种按顺序添加的方法,但我可以弄清楚如何按随机顺序添加。

关于如何完成随机顺序的任何想法?这是我的小提琴:

$("li").each(function(i,el) {
  var $this = $(this);
  var random = Math.floor(Math.random() * $this.length);
  setTimeout(function() {
    $this.eq(random).addClass('active');
  }, i*1000); // milliseconds
});

http://jsfiddle.net/1wgwajcd/1/

【问题讨论】:

    标签: jquery random


    【解决方案1】:

    这样的事情应该这样做

    (function hl() {
        var li = $('li'),
            r  = Math.floor(Math.random() * li.length),
            h  = li.eq(r).hasClass('active'),
            w  = li.filter('.active').length;
    
        li.eq(r).addClass('active');
    
        if (w < li.length) setTimeout(hl, h ? 0 : 1000);
    })();
    

    FIDDLE

    【讨论】:

    • 很好,但我遇到了问题。你能看看这个小提琴,看看你是否能想到问题可能是什么? jsfiddle.net/mBPug/35
    猜你喜欢
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多