Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

I'm trying to do a slider with jQuery

I have a bit of troubles, when I hit the previous button it's suppose to go to the last slide if i'm on the first. (It works with the next button but not with the previous) Anyone knows what's wrong?

  // Previous button is clicked
  $('span.prev').on('click', function() {
    // remove class to current item and add class oldActive to know this was the last slide shown
    $('.slide.active').removeClass('active').addClass('oldActive');
    // if oldActive was the last-child go to the first one
    if ( $('.oldActive').is(':first-child') ) {
      $('.slide').last().addClass('active');
    } else {
      // if oldActive was not the last-child, then add class to the previous one
      $('.oldActive').prev().addClass('active');
    }
    // Clear oldActive class
    $('.oldActive').removeClass('oldActive');
    // 
    $('.slide').fadeOut(speed);
    $('.slide.active').fadeIn(speed);
  });

Here is the pen http://codepen.io/Jesusz0r/pen/mVdxEL?editors=101

1 Answer

It appears you were able to get it working in the CodePen.

Yeah I did after hours hahaha