0
\$\begingroup\$

I'm trying to come up with the most basic example of making a JQuery slide show where you click on the current image you're viewing and you cycle through a gallery of photos. I know its probably not the most basic example, because if I want to add a new image I have to code more JQuery. Is there a more abstract approach where I don't have to code JQuery in terms of div id's and let classes take care of the work? Here is my JQuery

$(document).ready(function() {
$("#pic1").click(function() {
 $("#pic1").hide();
 $("#pic2").show(); 
}); 
$("#pic2").click(function() {
 $("#pic2").hide();
 $("#pic3").show(); 
});
$("#pic3").click(function() {
 $("#pic3").hide();
 $("#pic1").show(); 
});
});

The rest is here. http://jsfiddle.net/XjdTX/3/

asked Dec 10, 2012 at 6:00
\$\endgroup\$
1
  • \$\begingroup\$ You should include the main code in the question. \$\endgroup\$ Commented Dec 10, 2012 at 13:41

2 Answers 2

3
\$\begingroup\$

This should do the trick:

http://jsfiddle.net/XjdTX/11/

I changed the <div id="slideframe">...</div> to use a class instead. This will allow you to have multiple slide shows functioning off the same code, as shown in the jsFiddle. For toggling through the pics, the pic class is used and cycles based on the index of the clicked image.

There is also code in the jsFiddle that will fade the images instead of just toggling their visibility.

$(document).ready(function() {
 $('.slideframe').each(function() {
 var $pics = $(this).find('.pic'),
 max = $pics.length;
 $pics.on('click.slideframe', function(e) {
 var idx = $pics.index(this);
 if (idx < 0) {
 return false;
 }
 $(this).hide();
 $pics.eq(++idx % max).show();
 });
 });
});
answered Dec 10, 2012 at 8:39
\$\endgroup\$
1
  • 3
    \$\begingroup\$ You should use $pics.on('click', handler) instead of $pics.click(handler). \$\endgroup\$ Commented Dec 10, 2012 at 13:44
3
\$\begingroup\$
$(document).ready(function() {
 // set display:none for all <img> tags except the first
 $('.pic:gt(0)').hide();
 // stores all matches for class="pic"
 var $slides = $('.pic');
 $slides.click(function(){
 // stores the currently-visible slide
 var $current = $(this); 
 if( $current.is($slides.last()) ) {
 $current.hide();
 $slides.first().show();
 } 
 // else, hide current slide and show the next one
 else {
 $current.hide().next().show(); 
 }
 });
});

Edit Changed to include element caching, as per ANeves's suggestion.

Full code here: http://jsfiddle.net/XRpeA/

answered Dec 10, 2012 at 9:19
\$\endgroup\$
3
  • 1
    \$\begingroup\$ You could suggest elements caching (e.g. $(this)). \$\endgroup\$ Commented Dec 10, 2012 at 16:13
  • \$\begingroup\$ @ANeves, I am about as far from being comfortable with JS as people come, so thanks for the suggestion. Did you mean something like this: jsfiddle.net/ta4YU/2 ? \$\endgroup\$ Commented Dec 11, 2012 at 1:16
  • 1
    \$\begingroup\$ Precisely that! \$\endgroup\$ Commented Dec 11, 2012 at 11:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.