javascript - Show next image in Array onClick -
i working on setting images can clicked through on screen arrows. have images showing via loop , array. have been able set when hover on smallimage preview main image change image. aka can hover on them see larger version.
my array within mongo model: listings.currentimages
my current code below works small image take on featured image when hovered on. how change code work on screen left/right arrows?
<% var imgsrc = awspath + listings.currentimages[0] %> <img id='mainpicture' class="image-resposive" src=<%=imgsrc%>> <div id='allimages'> <% for(var = 0; < listings.currentimages.length; i++ ) { %> <div class='smallerimages'> <% var imgsrc = awspath + listings.currentimages[i] %> <img class="small" src="<%= imgsrc %>"> </div> <% } %> </div>
hover feature:
$('.small').hover(function() { $('.small').removeclass('selectedimage') var src = $(this).attr('src'); $(this).addclass('selectedimage') $('#mainpicture').attr('src', src); });
assuming have 2 icons (next/prev) can define 2 following event handlers in order move right or left:
$('#nextarrow').on('click', function(e) { var anchestor = $('.small.selectedimage').closest('.smallerimages'); var nextimg = $('#allimages .smallerimages:first .small'); if (anchestor.next().length != 0) { nextimg = anchestor.next().find('.small'); } $('.small').removeclass('selectedimage'); var src = nextimg.attr('src'); nextimg.addclass('selectedimage'); $('#mainpicture').attr('src', src); }) $('#prevtarrow').on('click', function(e) { var anchestor = $('.small.selectedimage').closest('.smallerimages'); var nextimg = $('#allimages .smallerimages:last .small'); if (anchestor.prev().length != 0) { nextimg = anchestor.prev().find('.small'); } $('.small').removeclass('selectedimage'); var src = nextimg.attr('src'); nextimg.addclass('selectedimage'); $('#mainpicture').attr('src', src); })
Comments
Post a Comment