Advertisement
Guest User

Untitled

a guest
Oct 7th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function ($, window, undefined) {
  2.     $(function(){
  3.         var counter = 0, // to keep track of current slide
  4.             $items = $('.diy-slideshow figure'), // a collection of all of the slides, caching for performance
  5.             numItems = $items.length; // total number of slides
  6.  
  7.         // this function is what cycles the slides, showing the next or previous slide and hiding all the others
  8.         var showCurrent = function(){
  9.             var itemToShow = Math.abs(counter%numItems);// uses remainder (aka modulo) operator to get the actual index of the element to show  
  10.  
  11.             $items.removeClass('show'); // remove .show from whichever element currently has it
  12.             $items.eq(itemToShow).addClass('show');
  13.         };
  14.  
  15.         // add click events to prev & next buttons
  16.         $('.next').on('click', function(){
  17.             if(counter == $items.length - 1) {
  18.                 counter = 0;
  19.             } else {
  20.                 counter++;
  21.             }
  22.             showCurrent();
  23.         });
  24.         $('.prev').on('click', function(){
  25.             if(counter == 0) {
  26.                 counter = $items.length - 1;
  27.             } else {
  28.                 counter--;
  29.             }
  30.             showCurrent();
  31.         });
  32.  
  33.         // if touch events are supported then add swipe interactions using TouchSwipe https://github.com/mattbryson/TouchSwipe-Jquery-Plugin
  34.         if('ontouchstart' in window){
  35.             $('.diy-slideshow').swipe({
  36.                 swipeLeft:function() {
  37.                     counter++;
  38.                     showCurrent();
  39.                 },
  40.                 swipeRight:function() {
  41.                     counter--;
  42.                     showCurrent();
  43.                 }
  44.             });
  45.         }
  46.     });
  47. })(jQuery, window);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement