I have a simple image gallery of about 25-30 photos with a thumbnails strip What i want is to load only the image that i click the thumbnail for not to wait until all the 25-30 photos are loaded because it slows down my website. Any ideas how to do that?
Advertisement
Answer
First, put all 25-30 photos on the page in a way that won’t load them; the simplest way is to give them a CSS style of display:none
. Then, put all your thumbnails on the page, and add jQuery logic like: $('.thumbnail').on('click', function() {...
. The click handling function can then use jQuery’s show
to show the full images as appropriate.
In other words, something like:
(HTML)
JavaScript
x
6
1
<img src="someImage.png" class="mainImage" id="image1" style="display:none"/>
2
<img src="someImage2.png" class="mainImage" id="image2" style="display:none"/>
3
4
<img src="someImageThumb.png" class="thumbnail" id="thumb1"/>
5
<img src="someImageThumb2.png" class="thumbnail" id="thumb2"/>
6
(JS)
JavaScript
1
7
1
$('.thumbnail').click(function(e) {
2
var thumbId = $(e.target).attr('id');
3
var index = thumbId.substr(5); // strip the "thumb" from "thumb1" to get "1"
4
var imageId = "main" + index;
5
$('#' + imageId).show();
6
});
7