Solved

jquery randomly shuffles thumbnails

Posted on 2013-02-07
9
383 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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi Pyromanci, leakim971

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

Expert Comment

by:Pyromanci
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Refael
Comment Utility
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
Comment Utility
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
Comment Utility
<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
Comment Utility
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
Comment Utility
sorry for the delay... you are the best! thank you.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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)

763 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

9 Experts available now in Live!

Get 1:1 Help Now