Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

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!!!
0
Refael
Asked:
Refael
  • 4
  • 4
1 Solution
 
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
 
RefaelAuthor Commented:
Hi Pyromanci, leakim971

This does not really help me. But thanks.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
PyromanciCommented:
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
 
RefaelAuthor Commented:
sorry for the delay... you are the best! thank you.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now