Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Jesus Mendoza
Jesus Mendoza
23,274 Points

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

Melissa Austin
Melissa Austin
3,383 Points

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

Jesus Mendoza
Jesus Mendoza
23,274 Points

Yeah I did after hours hahaha