JSFiddle - React, Tailwind, and code Playground

by Caleb Evans

HTML

<h1>iPad Slider</h1>

<!-- Buttons -->
<a href='#' id='prev'>Prev</a>
 &bull; 
<a href='#' id='next'>Next</a>

<br />
<br />

<!-- Container which only shows one slide at a time -->
<div class='container'>
    <!-- The slider moves when changing slides -->
    <div class='slider'>
    <div class='slide'>
        <div class='box'>1</div>
        <div class='box'>2</div>
        <div class='box'>3</div>
    </div>
    <div class='slide'>
        <div class='box'>4</div>
        <div class='box'>5</div>
        <div class='box'>6</div>
    </div>
    <div class='slide'>
        <div class='box'>7</div>
        <div class='box'>8</div>
        <div class='box'>9</div>
    </div>
</div>
</div>

CSS

.container {
    position: relative;
    width: 350px;
    height: 120px;
    border-radius: 3px;
    background: #444;
    overflow: hidden;
    /* Ensure slides are next to one another */
    white-space: nowrap;
    font-size: 0pt;
}
.slide {
    padding: 10px;
    display: inline-block;    
}
.box {
    display: inline-block;   
    width:100px;
    height: 100px;
    margin: 0px 5px;
    border-radius: 3px;
    background: #fff;
    box-shadow: 0px 1px 2px #000;
    font-size: 24pt;
    font-weight: bold;
    line-height: 100px;
    text-align: center;
}
.slider {
    position: relative;
    left: 0px; 
    -webkit-transition: left 0.5s ease-in-out;    
    -moz-transition: left 0.5s ease-in-out;    
}

JavaScript

var $next = $('#next');
var $prev = $('#prev');
// The slider is actually longer than what you see; the gray container simply hides the rest of it (so it only shows one slide at a time
var $slider = $('.slider');
// A slide, in this case, is just a group of boxes
var $slides = $('.slide');
var nslides = $slides.length;    

// Get width of each slide
var width = parseFloat($('.slide').width());
// Keep track of slider position (in pixels)
var pos = 0;

// Click "Next" to advance slider
$next.bind('click', function() {
    // Ensure slider does not move past slides
    pos = Math.min(width*(nslides-1), pos+width);
    // Set relative position of slider to next slide
    $slider.css({
        left: -pos 
    });
});


$prev.bind('click', function() {
    // Ensure slider does not move past slides
    pos = Math.max(0, pos-width);
    // Set relative position of slider to previous slide
    $slider.css({
        left: -pos 
    });
});