jsFiddle here: http://jsfiddle.net/qBQD4/
It is pretty simple; I want the right arrow to move the images forward, the left arrow to move them back in a simple slide show. I have launched several slide shows on the page on which this code will be implemented, so I went along the .next () path, and not just specifying a unique div identifier. HTML:
<div class="media"> <div class="slideButtons"> <span class="prev"><</span> <span class="next">/ ></span> </div> <ul class="gallery" id="olympGallery"> <li><img src="http://i.imgur.com/8aA7W.jpg" alt="" title="" /></li> <li><img src="http://i.imgur.com/jE7vj.jpg" alt="" title="" /></li> <li><img src="http://i.imgur.com/L7lVg.jpg" alt="" /></li> </ul> </div>
And jQuery code:
var speed = 100; $(".prev").click(function() { var now = $(this).next("ul.gallery").children(":visible"), last = $(this).next("ul.gallery").children(":last"), prev = now.prev(); prev = prev.index() == -1 ? last : prev; now.fadeOut(speed, function() {prev.fadeIn(speed);}); }); $(".next").click(function() { var now = $(this).next("ul.gallery").children(':visible'), first = $(this).next("ul.gallery").children(':first'), next = now.next(); next = next.index() == -1 ? first : next; now.fadeOut(speed, function() {next.fadeIn(speed);}); }); $(".gallery li").click(function() { var first = $(this).parent().children(':first'), next = $(this).next(); next = next.index() == -1 ? first : next; $(this).fadeOut(speed, function() {next.fadeIn(speed);}); });
And finally, some CSS:
.prev, .next {position: relative; padding: 3px; font-size:50px; font-weight: 900; cursor:pointer; } .gallery li{display:none; list-style:none;} .gallery li:first-child {display:block;} .gallery img { max-height:550px }
The 3rd function works fine (clicking the image goes to the next in the series). However, the first two are completely destroyed. What have I done wrong here?
source share