jquery randomly shuffles thumbnails

Hello Experts,

I want to learn how to use interval in jQuery.
I have 40 small photos thumb of our team. In the page i want to show only 30 and then to use shuffle/interval (randomly shuffles thumbnails). Here is a good example. I could use that yet only need the shuffle/interval and not all the gallery functions.

If someone can direct/explain how this is done. taking 40 thumb showing only 30 and them mix them as in the example: http://tremaynechrist.co.uk/Photofy  ... see how the thumb start to  shuffle.

Hope to find an EXPERT! Thanks!!!
RefaelAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
PyromanciConnect With a Mentor Commented:
ok.

You can leave your div structure the way it is now, and just do something like:

(function($) {
	$.randomize = function(arr) {
		for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
		return arr;
	};
})(jQuery);

var imagesSrc = [{"ImageUrl":"/Path/Th.... "},{"ImageUrl":"/Path/Th.... "}]; // your list of 40
function tick() {
	var randomImages = $.randomize(imagesSrc);
	while(randomImages.length>30) randomImages.splice(Math.random() * randomImages.length, 1);

	//do Display
	var x=0;
	for (var row=1;row < 7;row++) {
		for (var cell=0;cell<6;cell++) {
			$('.team-thumb-group'+row).children('img:eq('+cell+')').attr('src', randomImages[x].ImageUrl); // Change the children select to something better.
			x++;
		}
	}	
}

setInterval(tick, 30000);

Open in new window

As you can see you will still what to hold the image sources in an array.  as you have 40 originally and only want to display 30. So to still be able to display those image you will want to store thier paths. You can also change the cell and row vars in the for loop to match what you really want. I did it that way as the 30 images at 4 per row comes to 7.5 rows so i just made it 5 images per row for this little code sample. Which makes 6 even rows.

Also keep in mind i have not really tested, beyond syntax errors.
0
 
PyromanciCommented:
There are a few good examples already floating around the net you can use to learn from.

Here is some information on useing the setInterval it contains some code smaples and more help links.
http://stackoverflow.com/questions/914951/show-and-hide-divs-at-a-specific-time-interval-using-jquery

For the shuffling there is a shuffle plugin that you could use to see how they did it: http://mktgdept.com/jquery-shuffle
0
 
leakim971PluritechnicianCommented:
You may use :
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/random

var images = [{"ImageUrl":"/Path/Th.... "}]; // your list of 40
while(images.length>30) images.splice(Math.random() * images.length, 1);
// now you have only 30;

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
RefaelAuthor Commented:
Hi Pyromanci, leakim971

This does not really help me. But thanks.
0
 
PyromanciCommented:
Then if you could be a little bit more clear? You want to do what photofy is doing, but with out the gallery stuff, which I would assume based on the rest of your question is just randomly selecting images from  a list and re-displaying them at a set interval.

Basically you can take the information we have both provided and do just that. Something really quick based on the information already provided:

(function($) {
	$.randomize = function(arr) {
		for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
		return arr;
	};
})(jQuery);

var imagesSrc = [{"ImageUrl":"/Path/Th.... "}]; // your list of 40
function tick() {
	var randomImages = $.randomize(imagesSrc);
	while(randomImages.length>30) randomImages.splice(Math.random() * randomImages.length, 1);

	//do Display
}

setInterval(tick, 30000); //Fire tick function ever 30seconds

Open in new window



The only thing we have not supplied is how to animate or display any of it. Which if you really want it to do all the fancy stuff that photofy is doing with out the gallery, which i'm still not exactly sure what you mean by that, you would just be better off using photofy.
0
 
RefaelAuthor Commented:
Pyromanci thank you,

so if i have a div container for the entire thumb and a div for each thumb how do i integrate it with your script above? or should i remove the div around the thumb?
0
 
PyromanciCommented:
Are you talking something like:
<div id="thumb1">
	<div id="thumb1sub">
		<img>
	</div>
</div>
<div id="thumb2">
	<div id="thumb2sub">
		<img>
	</div>
</div>
<div id="thumb3">
	<div id="thumb3sub">
		<img>
	</div>
</div>
....

Open in new window

or
<div id="thumbcontainer">
	<div id="thumb1">
		<img>
	</div>
	<div id="thumb2">
		<img>
	</div>

	<div id="thumb3">
		<img>
	</div>
</div>
....

Open in new window

0
 
RefaelAuthor Commented:
<div id="team-thumb-container">

   <div class="team-thumb-group1">
   in each group i have 4 images here  
   </div>

   <div class="team-thumb-group2">
   in each group i have 4 images here  
   </div>

  etc............



</div>

Open in new window

0
 
RefaelAuthor Commented:
sorry for the delay... you are the best! thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.