jquery - How to go next img element by javascript -
i have many images in page.i want when click on images,image displayed on other large img tag(it's right work).then,i show next image when click next button , show previous image when click prev button.i tried ,but doesn't work properly.can me?thanks. please see code in below css code :
<style> .slideshow { position: relative; } .slideshow img { position: absolute; } .show{}/*complete in future*/ </style>
html code :
<div class="slideshow"> <img src="http://placekitten.com/200/300" width="100" height="100" alt="first image"> <img src="http://placekitten.com/200/287" width="100" height="100" alt="second image"> <img src="http://placekitten.com/200/285" width="100" height="100" alt="third image"> <img src="http://placekitten.com/200/286" width="100" height="100" alt="fourth image"> ... </div> <img id="largeimage" src="#" width="200" height="200" alt="big image"> <button type="button" id="prev"> previous</button> <button type="button" id="next">next</button>
my js code is:
$(document).ready(function() { /*this function set large img tag src attribute*/ $('.slideshow img').click(function() { var src = $(this).attr("src"); $('#largeimage').attr({src: src}); }); /*next button*/ $('#next').click(function() { var $curr = $('.slideshow img.show'); var $next = ($curr.next().lenght) ? $curr.next() : $('slideshow img').first(); var src = $next.attr("src"); $("#largeimage").attr({src: src}); $curr.removeclass('show'); $next.addclass('show'); }); /*previouse button*/ $('#prev').click(function() { var $curr = $('.slideshow img.show'); var $next = ($curr.next().lenght) ? $curr.next() : $('slideshow img').last(); var src = $next.attr("src"); $("#largeimage").attr({src: src}); $curr.removeclass('show'); $next.addclass('show'); }); });
you can see in jsfiddle
$(document).ready(function() { // here images hide , 1st image displayed $('.slideshow img').hide(); $('.slideshow img:eq(0)').show(); /*this function set large img tag src attribute*/ $('.slideshow img').click(function() { var src = $(this).attr("src"); $('#largeimage').attr({src: src}); }); /*next button*/ $('#next').click(function() { var displayimagenumber = 0; // find index of image visible $.each($('.slideshow img'),function(key,value){ if($(value).css('display') == 'block'){ displayimagenumber = key; } }); var displayimage = ''; $('.slideshow img:eq('+displayimagenumber+')').hide(); if($('.slideshow img').length == (displayimagenumber + 1)){ displayimage = $('.slideshow img:eq(0)'); }else{ displayimage = $('.slideshow img:eq('+parseint(displayimagenumber+1)+')'); } $(displayimage).show(); var src = $(displayimage).attr("src"); $('#largeimage').attr({src: src}); }); /*previouse button*/ $('#prev').click(function() { $.each($('.slideshow img'),function(key,value){ if($(value).css('display') == 'block'){ displayimagenumber = key; } }); $('.slideshow img:eq('+displayimagenumber+')').hide(); var displayimage = ''; if(displayimagenumber == 0){ displayimage = $('.slideshow img:eq('+parseint($('.slideshow img').length-1)+')'); }else{ displayimage = $('.slideshow img:eq('+parseint(displayimagenumber-1)+')'); } $(displayimage).show(); var src = $(displayimage).attr("src"); $('#largeimage').attr({src: src}); }); });
Comments
Post a Comment