Solved

jquery randomly shuffles thumbnails

Posted on 2013-02-07
9
384 Views
Last Modified: 2013-02-24
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
Comment
Question by:Refael
  • 4
  • 4
9 Comments
 
LVL 5

Expert Comment

by:Pyromanci
ID: 38865048
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
 
LVL 82

Expert Comment

by:leakim971
ID: 38865160
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
 

Author Comment

by:Refael
ID: 38865282
Hi Pyromanci, leakim971

This does not really help me. But thanks.
0
 
LVL 5

Expert Comment

by:Pyromanci
ID: 38865641
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Refael
ID: 38865828
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
 
LVL 5

Expert Comment

by:Pyromanci
ID: 38865889
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
 

Author Comment

by:Refael
ID: 38865988
<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
 
LVL 5

Accepted Solution

by:
Pyromanci earned 500 total points
ID: 38867840
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
 

Author Closing Comment

by:Refael
ID: 38923763
sorry for the delay... you are the best! thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now