Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <ul class="slider-container" data-slider="cats">
  <li><img src="http://lorempixel.com/800/400/cats/1/" /></li>
  <li><img src="http://lorempixel.com/800/400/cats/2/" /></li>
  <li><img src="http://lorempixel.com/800/400/cats/3/" /></li>
  <li><img src="http://lorempixel.com/800/400/cats/4/" /></li>
  <li><img src="http://lorempixel.com/800/400/cats/5/" /></li>
  <li><img src="http://lorempixel.com/800/400/cats/6/" /></li>
  <li><img src="http://lorempixel.com/800/400/cats/7/" /></li>
  <li><img src="http://lorempixel.com/800/400/cats/8/" /></li>
</ul>

<ul class="slider-container" data-slider="nature">
  <li><img src="http://lorempixel.com/800/400/nature/1/" /></li>
  <li><img src="http://lorempixel.com/800/400/nature/2/" /></li>
  <li><img src="http://lorempixel.com/800/400/nature/3/" /></li>
  <li><img src="http://lorempixel.com/800/400/nature/4/" /></li>
</ul>

<div data-slider-id="cats" data-slide="4">Cats Slide 4</div>
<div data-slider-id="nature" data-slide="2">Nature Slide 2</div>
              
            
!

CSS

              
                .slider-wrapper {
  width: 80%;
  margin: 0 auto;
  overflow: hidden;
}

.slider-container {
    height: 100%;
    padding: 0;
    margin: 0;
    
    position: relative;
    
    @extend .clearfix;

    transition-property: transform;
    transition-timing-function: ease-out;

    li {
        display: block;
        float: left;
        height: 100%;
        position: relative;

        &.next:first-child {
            left: 100%;
        }

        &.prev.last-child {
            right: 100%;
        }

        &.target:first-child, &.target.last-child {
            left: 0;
            right: 0;
        }
    }
}

.slide-prev, .slide-next {
    user-select: none;
    cursor: pointer;
  
    &.disabled {
        cursor: not-allowed;
        opacity: 0.5;
    }
}

.slider-pager {
    a.active {
        font-weight: bold;
        color: red;
    }
}

img {
  width: 100%;
}
 
.clearfix {
  *zoom: 1;
  
  &:before, &:after {
    content: " ";
    display: table;    
  }
  
  &:after {
    clear: both;
  }
}
              
            
!

JS

              
                +(function ($) {

    'use strict';

    var $win = $(window),
        
        // touch events
        flickTimeout = 400,
        swipeThreshold = 10,
        touchMoving = false,
        swiping = false,
        isFlick = false,
        
        startX, startY, deltaX, deltaY,

        // transition end events
        transition    = Modernizr.prefixed('transition'),
        transitioning = false,
        transitionend = {
          'WebkitTransition': 'webkitTransitionEnd',
          'MozTransition': 'transitionend',
          'OTransition': 'oTransitionEnd otransitionend',
          'msTransition': 'MSTransitionEnd',
          'transition': 'transitionend'
        }[transition],

        // support for css transitions
        support = Modernizr.csstransitions;


    var Slider = function (slider, options) {
        
        var self = this;
        
        ////////////////////////
        // Options Are Cool
        this.options = $.extend({
            controlNav: true,
            pagerNav: true,
            endless: true,
            speed: 300
        }, options);
      
        ////////////////////////
        // Set Up Properties
        this._isSliding = false;
        this._currIndex = 0;
      
        this._$slider = slider;
        this.sliderId = slider.data('slider');
        
        this._$wrapper = $('<div class="slider-wrapper"/>');
        this._$slider.wrap(this._$wrapper);
      
        this._$slides = this._$slider.children('li');
        this._slideCount = this._$slides.length;
        this._sliderWidth = this._slideCount * 100;
        
        // calc slider wrapper width
        this._$slider.width(this._sliderWidth + '%');
        // calc slide widths
        this._$slides.width(100 / this._slideCount + "%");
        
        ////////////////////////
        // Set Up Control Nav
        if( this.options.controlNav ) {
            this._$controlNav = $('<nav class="slider-nav"/>').insertAfter(this._$slider);
            this._$prevSlide  = $('<span class="slide-prev">←</span>').appendTo(this._$controlNav);
            this._$nextSlide  = $('<span class="slide-next">→</span>').appendTo(this._$controlNav);
        }
      
        ////////////////////////
        // Set Up Pager Nav
        if( this.options.pagerNav ) {
            this._$pagerNav = $('<nav class="slider-pager"/>').insertAfter(this._$slider);
            
            this._$slides.each(function(i) {
                $('<a>' + (i+1) + '</a>').appendTo(self._$pagerNav)
                .on('click', function(e) {
                    e.preventDefault();
                    self.captureMove(i);
                });
            });
          
            this._$pages = this._$pagerNav.children('a');
            
            // add initial active state
            this._$pages.eq(0).addClass('active');

            if( this.options.endless === false ) {
                this._$prevSlide.addClass('disabled');
            }
        }
        
        ////////////////////////
        // Call Init Methods
        this.attachEvents();
        
        // arrange slides & apply proper classes
        this.arrange(this._$slides.eq(0));
    };


    ////////////////////////
    // Attach Events To Slider
    Slider.prototype.attachEvents = function () {
        
        var self = this;
        
        // move to prev slide
        this._$prevSlide.on('click', function (e) {
            e.preventDefault();
            self.prev();
        });
        
        // move to next slide
        this._$nextSlide.on('click', function (e) {
            e.preventDefault();
            self.next();
        });
      
        // keyboard functionality
        // TODO: add click focus to control only one slider at a time
        $(document).on('keydown keyup', function (e) {
          
          // move to prev slide with left arrow
          if(e.which === 37) {
              e.preventDefault();
              self.prev();
          }
          
          // move to next slide with right arrow
          if(e.which === 39) {
              e.preventDefault();
              self.next();
          }
        });
        
        // move to specific slide
        $('[data-slide]').on('click', function () {
            
            var slider = $(this).data('slider-id'),
                slide  = parseInt($(this).data('slide'));
            
            if( slider === self.sliderId ) {
                self.captureMove(slide-1, false, true);
            }
        });
        
        // bind touch events
        this._$slider.on('touchstart', this.touchStart.bind(this));
    };


    ////////////////////////
    // Move To Previous Slide
    Slider.prototype.prev = function () {
        this.captureMove(this._$slides.filter('.prev').index(), -1);
    };


    ////////////////////////
    // Move To Next Slide
    Slider.prototype.next = function () {
        this.captureMove(this._$slides.filter('.next').index(), 1);
    };


    ////////////////////////
    // Capture Direction
    Slider.prototype.captureMove = function (index, direction, instant) {
        
        var self = this;
      
        // reinitialize slides
        function reInitSlides() {
          
            // arrange slides
            self.arrange(self._$slides.eq(newIndex));

            // stopped sliding
            self._isSliding = false;

            // check if we are moving to next or previous slide and mimic endless slide
            if ( (direction === 1 && newIndex === 0) || (direction === -1 && newIndex === self._slideCount - 1) ) {
                self.slide(0, true);
            }
        }
        
        // check if we need to move instantly
          instant = (typeof instant !== 'undefined') ? instant : false;

        // if not a number, on the same index or sliding then bail
        if ( typeof index !== 'number' || index === this._currIndex || this._isSliding ) return;
        
        // if we don't want it to be endless and we are on the first or last slides then bail
        if( this.options.endless === false ) {
          
            if( this._currIndex >= this._slideCount-1 && direction === 1 ) {
                this._currIndex = this._slideCount-1;
                return;
            }

            if( this._currIndex === 0 && direction === -1 ) {
                this._currIndex = 0;
                return;
            }

            if( this.options.controlNav ) {

                var targetNav = index === 0 ? this._$prevSlide : this._$nextSlide;

                // remove disabled class from previous controller
                this._$prevSlide.add(this._$nextSlide).removeClass('disabled');

                if( index === 0 || index >= this._slideCount-1 ) {
                    // add disabled class to previous controller
                    targetNav.addClass('disabled');
                }
            }
        }
          
        // now we are sliding
        this._isSliding = true;

        var newIndex   = index % this._slideCount,
            $currSlide = this._$slides.filter('.current');
        
        // set current index
        this._currIndex = $currSlide.index();
        
        console.log(index);
        console.log(this._currIndex);

        // if no direction specified grab the newIndex
        if (!direction) {
            this._$slides.eq(newIndex).addClass('target');
        }
        direction = direction || newIndex - this._currIndex;

        // move slide to new index
        this.slide(direction, instant);
        
        // if we're moving instantly we need to reinitialize slides
        if(instant) {
            reInitSlides();
        }

        // after transition has completed do shiz
        this._$slider.one(transitionend, reInitSlides);

        // update pagination with new index
        this._$pages.length && this._$pages.removeClass('active').eq(newIndex).addClass('active');
    };


    ////////////////////////
    // Perform The Slide
    Slider.prototype.slide = function (direction, instant) {
        console.log(this._currIndex);
        var x = -(direction + this._currIndex) * 100,
            s = instant ? 0 : this.options.speed;
      
        // if transforms & transitions are supported
        if (!!transition && Modernizr.csstransforms3d) {
          
          this._$slider.css({
            transform: 'translate3d(' + x / this._slideCount + '%, 0, 0)',
            transitionDuration: s + 'ms'
          });
        }

        // if transforms & transitions are not supported
        else {

          this._$slider.animate({
            left: x + '%'
          }, s, $.proxy(function () {
            s && this._$slider.trigger(transitionend);
          }, this));
        }
    };


    ////////////////////////
    // Arrange Slides
    Slider.prototype.arrange = function ($currSlide) {
        
        var $prevSlide = $currSlide.prev().length ? $currSlide.prev() : this._$slides.last(),
            $nextSlide = $currSlide.next().length ? $currSlide.next() : this._$slides.first(),
            lastChild  = this.options.endless ? 'last-child' : '';
        
        // set new index
        this._currIndex = $currSlide.index();

        // remove all classes
        this._$slides.removeClass('prev current next target last-child');

        // add "prev" class
        $prevSlide.addClass('prev')
            // if we are on the last slide, add "last-child" class
            .filter(':last-child').addClass(lastChild);
      
        // add "current" class
        $currSlide.addClass('current');

        // add "next" class
        $nextSlide.addClass('next');
    };


    ////////////////////////
    // Touch Events
    Slider.prototype.isSwipe = function (threshold) {
        return Math.abs(deltaX) > Math.max(threshold, Math.abs(deltaY));
    };


    Slider.prototype.touchStart = function (e) {
            
        if (this._isSliding) return false;
        
          touchMoving = true;
          deltaX = deltaY = 0;
            
        if (e.originalEvent.touches.length === 1) {
          
            startX = e.originalEvent.touches[0].pageX;
            startY = e.originalEvent.touches[0].pageY;

            this._$slider.on('touchmove touchcancel', this.touchMove.bind(this)).one('touchend', this.touchEnd.bind(this));

            isFlick = true;

            window.setTimeout(function () {
                isFlick = false;
            }, flickTimeout);
        }
    };


    Slider.prototype.touchMove = function (e) {
            
        deltaX = startX - e.originalEvent.touches[0].pageX;
        deltaY = startY - e.originalEvent.touches[0].pageY;

        if(this.isSwipe(swipeThreshold)) {
            e.preventDefault();
            e.stopPropagation();
            swiping = true;
        }
        if(swiping) {
            this.slide(deltaX / this._sliderWidth, true);
        }
    };


    Slider.prototype.touchEnd = function () {
            
        var threshold = isFlick ? swipeThreshold : this._sliderWidth / 2;

        if (this.isSwipe(threshold)) {
            (deltaX < 0) ? this.prev() : this.next();
        }
        else {
            this.slide(0, !deltaX);
        }

        swiping = false;

        this._$slider.off('touchmove', this.touchMove).one(transitionend, $.proxy(function() {
            this.slide(0, true);
            touchMoving = false;
        }, this));
      
      	return true;
    };
  

    ////////////////////////
    // Init Sliders
    $('[data-slider]').each(function() {
        new Slider($(this));
    });

}(jQuery));
              
            
!
999px

Console