Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function ($, window, undefined) {
- $(function(){
- var counter = 0, // to keep track of current slide
- $items = $('.diy-slideshow figure'), // a collection of all of the slides, caching for performance
- numItems = $items.length; // total number of slides
- // this function is what cycles the slides, showing the next or previous slide and hiding all the others
- var showCurrent = function(){
- var itemToShow = Math.abs(counter%numItems);// uses remainder (aka modulo) operator to get the actual index of the element to show
- $items.removeClass('show'); // remove .show from whichever element currently has it
- $items.eq(itemToShow).addClass('show');
- };
- // add click events to prev & next buttons
- $('.next').on('click', function(){
- if(counter == $items.length - 1) {
- counter = 0;
- } else {
- counter++;
- }
- showCurrent();
- });
- $('.prev').on('click', function(){
- if(counter == 0) {
- counter = $items.length - 1;
- } else {
- counter--;
- }
- showCurrent();
- });
- // if touch events are supported then add swipe interactions using TouchSwipe https://github.com/mattbryson/TouchSwipe-Jquery-Plugin
- if('ontouchstart' in window){
- $('.diy-slideshow').swipe({
- swipeLeft:function() {
- counter++;
- showCurrent();
- },
- swipeRight:function() {
- counter--;
- showCurrent();
- }
- });
- }
- });
- })(jQuery, window);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement