Javascript Image Gallery Using Mootools

Be sure to check out the second iteration of the image gallery.

This free image gallery based on javascript is a quick & easy solution. Implementing the demo doesn’t requre knowledge of any javascript. In this article, I’ll explain and breakdown the javascript that runs the image gallery using the javascript framework Mootools.

Demo:

Doggie Image 1   

Image 1

 

Image 2   

Image 2

 

Dog in Window   

Image 3

 

Two Dogs   

Image 4

 

Dog on Leash   

Image 5

 

 

Click A Thumbnail Above

 

Download Image Gallery


Mootools LogoThe Javascript image gallery is built from Mootools, an open-source object-oriented Javascript framework. If you’ve seen some cool graphical interactions you wouldn’t expect on a web page, chances are they are Mootools or another similiar framework like jQuery.

The Javascript:

window.addEvent('domready', function() {
	var drop = $('large');
	var dropFx = drop.effect('background-color', {wait: false});
	$$('.item').each(function(item){
		item.addEvent('click', function(e) {
			drop.removeEvents();
			drop.empty();
			var a = item.clone();
			a.inject(drop);
			dropFx.start('7389AE').chain(dropFx.start.pass('ffffff', dropFx));
		});
	});
});

The HTML:

<div id="imagegallery">
	<div id="items">
		<div class="item">
			<img src="images/167461515_05807d9fdc.jpg" alt="image" />
			<p>Image 1</p>
		</div>
		<div class="item" >
			<img src="images/229805603_e84bcf23bf.jpg" alt="image" />
			<p>Image 2</p>
		</div>
		<div class="item">
			<img src="images/248397811_0e7f1991be.jpg" alt="image" width="373" height="500" />
			<p>Image 3</p>
		</div>
		<div class="item" >
			<img src="images/1058831139_3dbe9e47b9.jpg" alt="image" width="500" height="333" />
			<p>Image 4</p>
		</div>
		<div class="item" >
			<img src="images/860213842_62e9bc4bcc.jpg" alt="image" width="500" height="333" />
			<p>Image 5</p>
		</div>
	</div>
	<div id="large">
		<div class="info">Click A Thumbnail on the Left</div>
	</div>
	<!– Image Gallery by http://tutorialdog.com –>
</div>

How the Javascript Works

The javascript you see above was the only part I needed to create- the rest is provided by the mootools framework. The first line, “window.addEvent(’domready’, function() {” and the closing brackets at the end is require of all (almost all) mootools scripts. The two variables at the beginning (var drop & var dropFX) govern the element with id=”large” in the html and the background color of that element.

The next part is just a function to get the image from the ‘items’ part of the html to the ‘large’ part of the html. “$$(’.item’).each(function(item){” basically says that for each html element identified under the class ‘item’, apply the following conditions to element. Now there is only one condition which would be “item.addEvent(’click’, function(e) {”. This line of code says that when the item is clicked, apply the follow instructions in the functions brackets. If you wanted to, you could change the word ‘click’ to ‘moueover’ and instead of needing to click on each photo, you would simply need put the mouse over the thumbnail image to get the large image to appear and essential get the function to trigger.

The code inside that ‘click’ function essential clears the items in the ‘large’ element and creates a clone of that ‘item’ class that was clicked and puts (injects) it into the ‘large’ element in the html.

Conclusion

If you’re just looking for a quick yet cool javascript image gallery, then download the included zip file and tweak the css to your liking. If you want to find out more about Mootools and creating really cool web apps, then check out the mootool demos which shows a variety of cool uses and a lot more functionality.

61 Responses to “Javascript Image Gallery Using Mootools”

  1. Rob Says:

    Hey — how can this be modified to show thumbnails (separate files) and load the full images? Looks like right now it’s using the full image as thumbs, and just cloning that image in the viewer area..

  2. Devin Says:

    @Rob You could do that, but then you would have to hardwire into the javascript where each full size image file is located. I didn’t want to do that because when you wanted to add another image, you not only have to add to the html, but also the javascript. My preference was to make it simpler and quicker to set up.

  3. Stephen Says:

    Hey-Is there someway to make the java script automatically load the first image?

  4. Brian Says:

    @Stephen:
    You can load the first image by simply doing this:

    I’m wondering, is there a way fade the images in & out instead of flashing a background color? How would that be done?

    I’ve tried to do it like this, but the image starts at 100% opacity, fades out and then back in when clicking the thumbnails instead of simply starting at 0 opacity and then up to full opacity:

    window.addEvent(’domready’, function()
    {
    var drop = $(’large’);
    //var dropFx = drop.effect(drop {’opacity’:0, wait: false}); // wait is needed so that to toggle the effect,
    var fx = new Fx.Styles(drop, {duration:200, wait:false});

    $$(’.item’).each(function(item)
    {
    item.addEvent(’click’, function(e)
    {
    drop.removeEvents();
    drop.empty();
    var a = item.clone();
    a.inject(drop);
    fx.start({
    ‘color’: ‘#000000′,
    opacity: ‘0.5′
    }).chain(function() {
    // executes 1 seconds after completion of above effect
    fx.start.delay(0, this, {
    ‘opacity’: 1
    });
    });
    });

    });
    });

    This approach doesn’t work as well as I’d hoped. Any idea which part needs tweaking?

    Thanks!

  5. Brian Says:

    @Stephen:
    Sorry, looks like I can’t put html in here. Let’s try it this way… You can load the first image by simply doing this:

  6. Brian Says:

    …nope, can’t post code examples here I guess. Basically, just load an image in the div id “large”.

  7. humus Says:

    how did you get it to work in IE? I have the same code but doesn not work in IE.

  8. Altima Says:

    Im loving this script, it does exactly what I wanted to do, and is easy for me to use. I have no java exerience, but I do know enough CSS.
    However Ive got one question. So far, you click on the thumbnail, and it shows the larger versn. How can I have that larger version link to an even larger version (full resolution) in a separate window?

  9. Anderson Says:

    Really cool script. But my thumbnails really bother me, because the size of the thumbnails can just be really small and I can’t really change it ;S

  10. Devin Says:

    Anderson, check out the second part of the tutorial. I made a better version!
    http://tutorialdog.com/javascript-image-gallery-using-mootools-part-2/

  11. Ryan Says:

    Hi, This script is brilliant just wont work in IE7. Throws an error at me saying ‘Object doesn’t support property or method. Has anyone got any ideas on how to solve this problem? Any replies would be much appreciated.

  12. Devin Says:

    @Ryan I fixed Part 2 to work in IE.

  13. RC Says:

    I am trying to get this to work in firefox but it is not working. Any ideas?

  14. Jordan Says:

    I thought others might be interested in how I used this for a gallery implementation, with modifications for displaying different images in the large div, and also for linking again to a large version of the image.

    See my post about it:

    http://www.uncharteddesign.com/blog/?p=10

    Thanks Devin!

  15. A study in Ajax Web trends. What are the best Free Ajax Resources? (70 of the Best Ajax Resources). | Speckyboy - Wordpress and Design Says:

    [...] 9. Javascript Image Gallery [...]

  16. alanya Says:

    very good… Thanks you very much

  17. rock2008 Says:

    great job man u did a fabulous job………..

  18. prateek Says:

    its a great tool but please let me know how it can be use for dynamic images??????plz i am in urgency plz let me know

  19. Sankar Says:

    Hi, Thanks for the nice and useful script. Is it any way to pass the another image path to display? I mean thumbnail and display picture should be different? Is it possible?

  20. Petr Says:

    Hi, implement a simple click to view full resolution of image and I will call it great gallery :)

  21. Avsa Says:

    Great Post.Thank you

  22. Brick Says:

    Thanks a lot for this, it is very helpful for me…

  23. liliana Says:

    maravillosa libreriá y aplicacion….

  24. accounting homework Says:

    Good one, galleries can be useful at times especially for projects etc

    Thanks for posting them :)

  25. Javascript Image Gallery Using Mootools - Tutorial Collection Says:

    [...] View Tutorial No Comment var addthis_pub=”izwan00″; BOOKMARK This entry was posted on Monday, June 8th, 2009 at 2:02 am and is filed under Javascript Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. [...]

  26. Film School Says:

    Thanks, there is more reason to comment than ever before!

  27. rajiv menon Says:

    Great javascript image gallery. Thank u for sharing.

  28. Accounting Homework Says:

    Your scrip good and nice collection gallery.
    Thanks

  29. Nameer Says:

    Hi Friends,
    The script is really amazing. I have developed a imagegallery using the same. Now i want to create a video gallery in the same manner. Is there any script to do the same.If anybody can help would be highly appreciated

  30. Advice: The best JavaScript Image Galleries « Web Design Northampton Says:

    [...] Mootools Image Gallery http://tutorialdog.com/javascript-image-gallery-using-mootools/ [...]

  31. free style Says:

    very kool…more useful!!

  32. excel help Says:

    wow its amazing i was looking for that kind of brilliant designs…Thanks

  33. 20 Ejemplos de Galerias Web 2.0 @ Ciberdix 2.0 :: Blog Creativo!! Says:

    [...] TutorialDog Image Gallery | Demo [...]

  34. Javascript Image and Photo Galleries – Mootool, Prototype, jQuery and Tutorials | guidesigner.net Says:

    [...] TutorialDog Image Gallery | Demo [...]

  35. Guia de las mejores ejemplos en mootools y ajax, listos para descarga | El origen de Internet Says:

    [...] 8. Javascript Image Gallery [...]

  36. 16 Free JavaScript Solutions for Displaying Your Images | 9Tricks.Com - Tips - Tricks - Tutorials Says:

    [...] 15. Javascript Image Gallery Using Mootools [...]

  37. Jignesh Parmar Says:

    This Image Gallery is very very best in viewing and Imagination point of view. I have never think with Jscript this will be done….

    Thanx a lot for tutorial…….

  38. M.S.ENGINEERING Says:

    The Jscript is more useful for us to make up the image gallery using the mootools….. Very nice job…… Learned a lot from this….. Thanks for sharing….

  39. Annan Says:

    wow its amazing i was looking for that kind of brilliant designs…Thanks

  40. Annan Says:

    very cool script …more useful!!

  41. Free Javascript Web Gallery Solutions | Design Dazzling Says:

    [...] Javascript Image Gallery Using Mootools – This is a very simple image gallery, great for when you need something up right now. It doesn’t require you to know very much JavaScript to get it to work. [...]

  42. Gayathri Says:

    Nice collections….thanks for sharing….

  43. 30 Javascript Image and Photo Galleries – Mootool, Prototype, jQuery and Tutorials « HUE Designer Says:

    [...] TutorialDog Image Gallery | Demo [...]

  44. sean tarrant Says:

    hi! have been trying to add this photo gallery to my website using dreamweaver but it doesn’t work.

    i have the code just need to know wat to do step by step would be much appreciated

    many thanks
    sean tarrant

  45. Erick Says:

    Hi, just wonder if I can actually load the image automatically from specified browser without manually putting the picture path and name, using PHP maybe?

  46. sonia Says:

    very useful

  47. godwin Says:

    useful post

  48. seeli Says:

    really good

  49. jeba Says:

    Nice collections….thanks for sharing….

  50. jemi Says:

    wow its amazing. Thanks for sharing

  51. immanuel Says:

    good list. Thanks for posting

  52. mani Says:

    useful list. It is useful for me

  53. justin Says:

    It is useful for me. View it

  54. pricy Says:

    it is really good

  55. siru Says:

    Thanks for the list

  56. ravi Says:

    amazing list. . . very useful

  57. ramesh Says:

    useful article. . . thanks for info

  58. Image Gallery Mootools ???? - Ajax???? - Java???? - ???? - ajax?????? - Image-Gallery-Mootools - Java???? - ??? Says:

    [...] [...]

  59. 123doing Says:

    It’s very good.
    I like this.
    Thanks for share.
    And I wrote something to introduce this project for my readers.
    You can find the post about this in my website.
    If something is wrong,pls figure it out.thanks.

  60. jeba Says:

    Its very good article… Thanks for posting it

  61. raashi Says:

    Amazing articles….Thanks for posting it

Leave a Comment