javascript - 如何随机选择具有相同类别的元素?

标签 javascript jquery html css css-transitions

jquery 中是否有任何选项可以随机选择具有相同类名的 (3) 个元素?

实际上我只想向上滑动 3 个元素 应该是随机

function e(e) {
    var t = $(window).scrollTop(),
        n = t + $(window).height(),
        r = $(e).offset().top,
        i = r + $(e).height() * .8;
    return i >= t && r <= n && i <= n && r >= t
}

function s() {
    if (e(t) && !i) {
        r.each(function (e) {
            $(this).delay(200 + e * 50).animate({
                top: "-110%"
            }, 500)
        }).each(function (e) {
            $(this).delay(200 + e * 100).animate({
                top: "0%"
            }, 500)
        });
        i = !0
    }
    i && $(window).unbind("scroll", s)
}
var n = $("#thumbs"),
    t = $(".thumbnails"),
    r = n.find(".thumb-info"),
    i = !1;
s();
$(window).bind("scroll", s);

演示 http://jsfiddle.net/sweetmaanu/GgY3Z/

最佳答案

var items = $('.thumbnails');
var random = shuffle(items).slice(0, 3);

解释:

  1. $('.thumbnails') 选择类为 thumbnails
  2. 的所有元素
  3. > shuffle()从数组中返回 3 个随机元素(查看链接)
  4. .slice(0, 3) 获取打乱后数组的前 3 个值。

这是我为查看结果而制作的 fiddle : Fiddle

关于javascript - 如何随机选择具有相同类别的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18230760/

相关文章:

javascript - ajax onclick 监听器不起作用

php - 在 PHP 中使用 cookie 打印历史记录

javascript - 如何将代码移出主 server.js 文件,但在应用程序启动时仍然使用它?

php - 下载脚本非常慢

javascript - 使用 jQuery $.getJSON 处理 Twitter API 错误

javascript:程序卡住

javascript - 根据输入值默认选择选项

javascript - 使用 jQuery 更新 CSS 变换值

html - 将小图像放置在大图像周围的圆形阵列中

html - 不同的按钮大小取决于我使用的是哪个单元。为什么?