simple jquery image gallery
I have a simple jquery image gallery. When a user clicks on thumb it will replace ‘thumb’ to ‘large’ in src attribute of the main Image, I use 开发者_开发技巧delegate method of jQuery to dynamically load thumbs and use them without bind event listeners for new thumbs. jsfiddle
Now i need to be able to link to a specific image in the gallery, For example, example.com/gallery.php#3rdimage
so the user will immediately see the 3rd image in the gallery, something like the method that engadget is using http://www.engadget.com/photos/samsung-series-5-chrome-os-laptop-vs-11-inch-macbook-air-fight/#4128000
Simply grab the page's hash value and use it as an index against the set of thumbs:
wlh = window.location.hash[1];
if (!isNaN(wlh)) {
$('#largeImage').attr('src', $('#thumbs img').eq(wlh-1).attr('src').replace('thumb', 'large'));
}
Demo: jsfiddle.net/EbCKN/show/#3 (will initially show the 3rd image)
To add a hash to the URL while browsing images so the URL is primed for sharing a certain image, it's just an extra line after changing the #largeImage
:
window.location.hash = $('#thumbs img').index(this)+1;
Demo: jsfiddle.net/EbCKN/1/show/ (edit)
This is a simple jquery image gallery!
$(document).ready(function(){
$(".thumb").click(function() {
var dir = $(this).attr("src");
$('#image').hide();
$('#image').fadeIn('fast');
$('#image').attr("src",dir);
});
});
#image{
border:4px #666 solid;
height:480px;
width:640px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img id="image" src="http://www.981powerfm.com.au/images/stories/2014/09/happy_animal_5.jpg" border="0"/>
<img src="http://www.981powerfm.com.au/images/stories/2014/09/happy_animal_5.jpg" class="thumb" width='100px' />
<img src="http://smartyvet.com/site/wp-content/uploads/2014/05/happy5.jpg" class="thumb" width='100px' />
<img src="http://www.telegram.ee/wp-content/uploads/2013/11/a.aaa-Happy-animals.jpg" class="thumb" width='100px' />
精彩评论