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

General Discussion

Conrad Cortes
Conrad Cortes
8,418 Points

jquery

Does anyone know if there is a specific lesson I can view on here to build a jquery slider or slide show?

1 Answer

Aaron Elliott
Aaron Elliott
11,738 Points

Here's One that I am working on,

HTML

      <div class="slider" >


          <div class="slides">


          <div class="slide" id="slide_1" style="background-image:url(img/ducks.jpg); background-size:cover;">

            <div id="slide" class="slideContent">

            <h1 id="slideTitle">slide one</h1>

            </div>

          </div>

          <div class="slide" id="slide_1" style="background-image:url(img/mefrontpage.jpg); background-size:cover;">

            <div id="slide" class="slideContent">

            <h1 id="slideTitle">slide two</h1>

            </div>

          </div>

                    <div class="slide" id="slide_1" style="background-image:url(img/ducks.jpg); background-size:cover;">

            <div id="slide" class="slideContent">

            <h1 id="slideTitle">slide three</h1>

            </div>

          </div>

          <div class="slide" id="slide_1" style="background-image:url(img/mefrontpage2.jpg); background-size:cover;">

            <div id="slide" class="slideContent">

            <h1 id="slideTitle">really super long title example...</h1>

            </div>

          </div>

          </div>

          <li class="fb-btn left" id="sliderLeft">&#10094</li>

          <li class="fb-btn right" id="sliderRight">&#10095</li>

        </div>


        <div class="slide-buttons"></div>

JS (Citation JavaScript and jQuery by Jon Ducket )

$('.slider').each(function(){
    var $this = $(this);
    var $group = $this.find('.slides');
    var $slides = $this.find('.slide');
    var buttonArray = [];
    var currentIndex = 0;
    var timeout;
    var slideTimer = 8000;

    var $backButton = $this.find('#sliderLeft');
    var $forwardButton = $this.find('#sliderRight');

  function move(newIndex){
    var animateLeft, slideLieft;
    advance();
    if($group.is(':animated') || currentIndex === newIndex){
      return;
    }
    buttonArray[currentIndex].removeClass('active');
    buttonArray[newIndex].addClass('active');

    if (newIndex > currentIndex){
      slideLeft = '100%';
      animateLeft = "-100%";
    }else{
      slideLeft = '-100%';
      animateLeft = '100%';
    }
    $slides.eq(newIndex).css( {left: slideLeft, display: 'block'} );

    $group.animate( {left: animateLeft} , function(){
      $slides.eq(currentIndex).css({display:'none'});
      $slides.eq(newIndex).css({left:0});
      $group.css({left:0});
      currentIndex = newIndex;
    });
  }
 function advance() {                   
    clearTimeout(timeout);           
    timeout = setTimeout(function() {     
      if (currentIndex < ($slides.length - 1)) { 
        move(currentIndex + 1);          
      } else {                           
        move(0);                       
      }
    }, slideTimer);                              
  }
      $backButton.on('click', function(){

          if(currentIndex === 0){
            move(3)
          }else{
             move(currentIndex - 1);
          }
      });
            $forwardButton.on('click', function(){
                  if(currentIndex === 3){
            move(0)
          }else{
             move(currentIndex + 1);
          }
      });
  $.each($slides, function(index){

    var $button = $('<button type="button" class="slide-btn">&bull;</button>');
    if (index === currentIndex){
      $button.addClass('active');
    }
    $button.on('click', function(){
      move(index);
    }).appendTo('.slide-buttons');
    buttonArray.push($button);
    });
  advance();
  });

CSS

/*SLIDER CSS*/
.fb-btn{
    color:#fff; 
    list-style: none;
    width:12%;
    padding-top: 325px;
    padding-bottom: 325px;
    position:absolute;
    top:0px;
    text-align: center;
    font-size: 32px;
    cursor: pointer;

}
.fb-btn:hover{ 
    background-color:rgba(0,0,0,0.25);

}
.left{
    left:0px;
}
.right{
    right:0px;
}
.slide-buttons{
    text-align:center;
    margin:0 auto;
    position:relative;
    margin-top:-44px;
    max-width:310px;
}
.slide-btn{
    font-size: 3em;
    width: 50px;
    border:none;
    padding: 0;
    margin: 0;
    background-color: rgba(245,245,245,0.0);
    color: #EEEEEE;
}
.slide-btn:hover{

    color: #AAAAAA;

}
.active{
    color:#f2592a;
}
.active:hover{
    color:#f9a23e;
}
.slider{
    position:relative;
    overflow: hidden;
    height: 650px;
    border-bottom: solid medium #F0F0F0;
    background-color: #000;

}
.slides{
    width:100%;
    height:100%;
    position:relative;

}
.slide{
    width:100%;
    height: 100%;
    display:none;
    position:absolute;
}
.slideContent{
            margin-left: auto;
        margin-right: auto;
        width: 76%;
        max-width:1100px;
        font-family: 'Oswald', sans-serif;
}
.slide:first-child{
    display:block;
}
#slide{
    height: 700px;
    position: relative;
    margin: 0 auto;
}

#slideTitle{
    color: #FBFBFB;
    padding: 10px;
    font-size: 4em;
    position: absolute;
    bottom:150px;
    font-variant: small-caps;
}

..really should get a codepen account