simple-slider
  • Blog
  • Docs
  • Updates
Docs Menu
  • Examples
    • Custom ease animation
    • Default Sliding Transition
    • Default slider with no options
    • Fading/Opacity transition
    • Full page transition
    • Lightbox Integration
    • Mask Sliding transition
    • Pause slide
    • RequireJS usage
    • Responsive Layout Example
    • Reverse sliding direction
    • Right to left sliding transition
    • Slides containing no image
    • Top to bottom transition
    • Touch swipe example
  • API
simple-slider
  • Blog
  • Docs
  • Updates

Touch swipe example Guide

Example showing how to use a touch/swipe interface to control the slider.

View this example

<!doctype html>
<html>
  <head>
    <title>Touch swipe example</title>
    <style>
      .slider { width: 100%; padding-bottom: 25%; }
      .slider img { width: 100%; }
      .wrapper { width: 1024px; margin: auto; }
      @media only screen and (max-width: 1024px) {
        .wrapper { width: 100%; margin: 0; }
      }
    </style>
  </head>
  <body>
    <div class="wrapper">
      <div id="myslider" class="slider">
        <img src="http://placekitten.com/g/1024/256"/>
        <img src="https://unsplash.it/1024/256"/>
        <img src="https://unsplash.it/1024/256?random=1"/>
        <img src="https://unsplash.it/1024/256?random=2"/>
      </div>
      <p>This example is using the <a href="https://github.com/hammerjs/hammer.js">Hammer.js</a> lib to provide the swipe gesture support.</p>
    </div>
  </body>
  <script src="https://rawgit.com/ruyadorno/simple-slider/master/dist/simpleslider.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.min.js"></script>
  <script>
    var isNext = true;
    var sliderElem = document.getElementById('myslider');
    var slider = simpleslider.getSlider({
      container: sliderElem
    });
    var manager = new Hammer.Manager(sliderElem);
    var Swipe = new Hammer.Swipe();

    manager.add(Swipe);

    manager.on('swipeleft', function() {
      if (isNext) {
        slider.reverse();
        isNext = false;
      }
      slider.next();
    });

    manager.on('swiperight', function() {
      if (!isNext) {
        slider.reverse();
        isNext = true;
      }
      slider.next();
    });
  </script>
</html>

Contribute on Github! Edit this section.