0

is there any option in jquery to randomly select number(3) of elements with in the same class name ?

Actually I just wanna slideup up 3 items only which should be randomly

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);

DEMO http://jsfiddle.net/sweetmaanu/GgY3Z/

Mo.
  • 26,306
  • 36
  • 159
  • 225

4 Answers4

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

Explanation:

  1. $('.thumbnails') selects all elements with class thumbnails
  2. shuffle() returns 3 random elements from the array (check out the link)
  3. .slice(0, 3) grabs the first 3 values of the shuffled array.

Here's a Fiddle I made to see the result: Fiddle

Community
  • 1
  • 1
Broxzier
  • 2,909
  • 17
  • 36
0

Use a shuffle algorithm, for example

function shuffleArray(a) { // Fisher-Yates shuffle, no side effects
    var i = a.length, t, j;
    a = a.slice();
    while (--i) t = a[i], a[i] = a[j = ~~(Math.random() * (i+1))], a[j] = t;
    return a;
}

Shuffle your Array of elements;

myArray = shuffleArray(myArray);

Now the first 3 items are random, so loop over them with your slide.

var i;
for (i = 0; i < 3 && i < myArray.length; ++i) {
    // slide myArray[i]
}
Paul S.
  • 64,864
  • 9
  • 122
  • 138
0

this is pure js and does that what you need

var fys=function(d,a,b,c){ //FisherYatesShuffle
 a=d.length;
 while(--a){
  b=Math.floor(Math.random()*(a-1));
  c=d[a];d[a]=d[b];d[b]=c;
 }
};
var el=document.getElementsByClassName('thumbnails'),l=el.length;
var n=[];while(l--){n[l]=l};
fys(n)

n now contains the array with the random indexes and you use

el[n[0]] to call the first random element.

cocco
  • 16,442
  • 7
  • 62
  • 77
  • What prevents this from choosing the same item all 3 times, resulting in only one item being moved? – Paul S. Aug 14 '13 at 11:52
  • This also has an extremely small chance that `el[el.length]` will be selected, which is undefined. – Broxzier Aug 14 '13 at 11:54
  • The best way to explain something to people is always to use the shortest variables names possible, so people clearly understand what a,b,c,d,e,f,g,h and i variables are set too, since those are easy to remember. **FALSE!** Use proper variable names when you're posting examples. Not everyone that's reading the answer is at ease with JavaScript yet. – Jeff Noel Aug 14 '13 at 12:23
  • the Fisher-Yates shuffle would be to long. the only 2 vars you need to read are el & n ... el contains the element collection with the class thumbnails, n contains an array with the shuffled indexes of the el.pls... – cocco Aug 14 '13 at 12:25
0

Simple for one element do this:

var items = $('.item').length;
var currentItem = Math.floor(Math.random() * (items + 1));
$('.item').eq(currentItem).doSomething();

For some elements:

var number = 10;
for(i=0; i <= number; i++) {
    $('.item').eq(randomElement('.item')).doSomething();
}

function randomElement(el){
    var items = $(el).length;
    var currentItem = Math.floor(Math.random() * (items + 1));
    return currentItem;
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322