Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jquery randomly shuffles thumbnails

Posted on 2013-02-07
9
Medium Priority
?
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

 
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
 

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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

730 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