On a recent client project I was tasked with building an infinite slider that moved in a diagonal direction at an angle of about 45 degrees. Using a couple of CSS tricks I was able to achieve the effect in an easy and quick way.

The markup

The mark up is really simple. A <div class="slider"> that will act as our container and then a series of <div class="slide"> for each slide. We also add class of active to the first slide.

Each slide can contain whatever content you like.

  <div class="slider">
  <div class="slide active">
    <h3>Slide One</h3>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, fuga itaque commodi. Quidem facere totam perspiciatis voluptatibus harum obcaecati blanditiis itaque in quos libero architecto, ratione tempore necessitatibus alias quam.</p>
  </div>
  <div class="slide">
    <h3>Slide Two</h3>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, fuga itaque commodi. Quidem facere totam perspiciatis voluptatibus harum obcaecati blanditiis itaque in quos libero architecto, ratione tempore necessitatibus alias quam.</p>
  </div>
  <!-- More slides -->
</div>

The CSS

Whilst my use case was creating a diagonal slider, in this example I will create a simple horizontal slider, and I'll talk about the angles at the end.

First thing to set up is the slider container with a position of relative and some size information, setting the overflow to hidden - Y'know, your classic slider container set up; vintage.

  .slider {
  position: relative;
  width: 600px;
  height: 300px;
  overflow: hidden;
}

Now for the slides. This is my favourite bit as it's all really empowered by some clever selectors.

First, set up the slides to take up the full size of the container and position them absolutely. Then, add a transition property so that the slides animate as they move.

Now for the cool stuff:

Set the slides to have a default transform property of translate(-100%, 0). This will position the slide 100% of its width to the left of its natural position (so, basically, off to the left of the slider).

Next, set the .active class to transform translate(0, 0) so it sits in its natural position (right in the middle of the slider).

Finally, the best bit: Use the general sibling selector ~ to position all of the slides that come after the active class to the right of the slider, setting the transform property to translate(100%, 0)

  .slide {
  width: 600px;
  height: 300px;
  position: absolute;

  // Add transition
  transition: 0.6s ease;

  // Move slides off to the left by default
  transform: translate(-100%, 0);
}

// Set active slide to be in the viewport
.slide.active {
  transform: translate(0, 0);
}    

// Use sibling selector to position all slides after the active slide to the right
.slide.active ~ .slide {
  transform: translate(100%, 0);
}

All we need to do now is move the .active class up and down our slides to navigate along the slider. For that, we'll need some JavaScript.

The JavaScript

It's up to you how you do this; it's just as achievable with vanilla JS, but I often think concepts are easier to get across with the plain English style of JQuery.

The infinite problem

So the tricky thing about building an infinite slider is the infinite bit.

To do this we're going to move the first and last slides to the start and end of the list respectively as we need them. So every time the user clicks 'next' the first slide in the list will jump to the end, meaning the user can keep clicking forever and never run out of slides. Similarly when the user clicks on 'previous' we'll move the last slide in the list to the start.

We'll also need to move the last slide to the start of the list on page load, so the user can click on previous straight away.

To move the last slide before the first we can use JQuery's before method:

  var first = $('.slide').first();
var last = $('.slide').last();
first.before(last)

And vice versa, to move the first slide after the last as we move forward we can use the after method:

  var first = $('.slide').first();
var last = $('.slide').last();
last.after(first)

NOTE: The eagle eyed amongst you may have realised that this won't work with less than 3 slides (you always need one slide before, one slide after, and one slide in the middle). To handle this in production I wrote a small recursive function to duplicate the slides list if less than 3 until there were over 3 slides in total.

With that sorted, we just need to move the .active class using the next() and prev() methods, depending on which button is clicked. Here's the full JavaScript:

  // Get all the slides
var slides = $('.slide');

// Move the last slide before the first so the user is able to immediately go backwards
slides.first().before(slides.last());

$('button').on('click', function() {
  // Get all the slides again
  slides = $('.slide');
  // Register button
  var button = $(this);
  // Register active slide
  var activeSlide = $('.active');

  // Next function
  if (button.attr('id') == 'next') {
    // Move first slide to the end so the user can keep going forward
    slides.last().after(slides.first());
    // Move active class to the right
    activeSlide.removeClass('active').next('.slide').addClass('active');
  }

  // Previous function
  if (button.attr('id') == 'previous') {
    // Move the last slide before the first so the user can keep going backwards
    slides.first().before(slides.last());
    // Move active class to the left
    activeSlide.removeClass('active').prev('.slide').addClass('active');
  }
});

Conclusion

By using CSS translate to position the slides, if we want to change the angle of entry and exit, we just need to adjust the X & Y values in the translate(). For a 45 degree angle I was using the following setup:

  
.slide {
// Position slides below the slider and 50% to the left
  transform: translate(-50%, 100%);
}
// Set active slide to be in the viewport
.slide.active {
  transform: translate(0, 0);
}  
// Use sibling selector to position all slides after the active slide 50% to the right and above the slider
.active ~ .slide {
  transform: translate(50%, -100%);
}


Feel free to fork the pen and have a play!


852 2 32