Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <div class="slider js-slider">
  <div class="slider__inner js-slider__inner">
    <div class="slide js-slide">
      <div class="slide__bg" style="background-image: url('https://placekitten.com/g/800/400');"></div>
    </div>
    <div class="slide js-slide">
      <div class="slide__bg" style="background-image: url('https://placekitten.com/g/800/400');"></div>
    </div>
    <div class="slide js-slide">
      <div class="slide__bg" style="background-image: url('https://placekitten.com/g/800/400');"></div>
    </div>
    <div class="slide js-slide">
      <div class="slide__bg" style="background-image: url('https://placekitten.com/g/800/400');"></div>
    </div>
    <div class="slide js-slide">
      <div class="slide__bg" style="background-image: url('https://placekitten.com/g/800/400');"></div>
    </div>
    <div class="slide js-slide">
      <div class="slide__bg" style="background-image: url('https://placekitten.com/g/800/400');"></div>
    </div>
    <div class="slide js-slide">
      <div class="slide__bg" style="background-image: url('https://placekitten.com/g/800/400');"></div>
    </div>
  </div>
</div>

<div class="text text--bottom-left">Drag to explore</div>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body, html{
  height: 100%;
}

body{
  display: flex;
  align-items: center;
  position: relative;
  overflow: hidden;
  background-color: #111;
  user-select: none;
}

img{
  display: block;
  width: 100%;
  height: auto;
}

.slider{
  position: relative;
  height: 100%;
  width: 100%;
  cursor: grab;
  
  &.is-grabbing{
    cursor: grabbing;
  }
}

.slider__inner{
  position: absolute;
  top: 0;
  left: 0;
  height: 100vh;
  pointer-events: none;
}

.slide{
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 60vw;
  height: 60vh;
  padding: 1rem;
}

.slide__bg{
  position: absolute;
  top: 10%;
  left: 10%;
  bottom: 10%;
  right: 10%;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

.slide__title{
  position: relative;
  color: #fff;
  font-size: calc(1rem + 3vw);
  font-weight: normal;
  z-index: 2;
}

.text{
  position: absolute;
  color: #666;
  left: 4rem;
  bottom: 3rem;
}

.cursor-wrap{
  position: fixed;
  top: 0;
  left: 0;
  pointer-events: none;
}

.cursor{
  position: fixed;
  top: -30px;
  left: -30px;
}
              
            
!

JS

              
                function _getClosest(item, array, getDiff) {
    var closest,
        diff;

    if (!Array.isArray(array)) {
        throw new Error("Get closest expects an array as second argument");
    }

    array.forEach(function (comparedItem, comparedItemIndex) {
        var thisDiff = getDiff(comparedItem, item);

        if (thisDiff >= 0 && (typeof diff == "undefined" || thisDiff < diff)) {
            diff = thisDiff;
            closest = comparedItemIndex;
        }
    });

    return closest;
}

function number(item, array) {
  return _getClosest(item, array, function (comparedItem, item) {
    return Math.abs(comparedItem - item);
  });
}
    
function lerp(a, b, n) {
    return (1 - n) * a + n * b
}

class Slider {
  constructor(options = {}) {
    this.bind()
    
    this.opts = {
      el: options.el || '.js-slider',
      ease: options.ease || 0.1,
      speed: options.speed || 1.5,
      velocity: 25,
      scroll: options.scroll || false
    }
    
    this.slider = document.querySelector('.js-slider')
    this.sliderInner = this.slider.querySelector('.js-slider__inner')
    this.slides = [...this.slider.querySelectorAll('.js-slide')]
    this.slidesNumb = this.slides.length
    
    this.rAF = undefined
    
    this.sliderWidth = 0
    
    this.onX = 0
    this.offX = 0
    
    this.currentX = 0
    this.lastX = 0
    
    this.min = 0
    this.max = 0

    this.centerX = window.innerWidth / 2
  }
  
  bind() {
    ['setPos', 'run', 'on', 'off', 'resize'].forEach((fn) => this[fn] = this[fn].bind(this))
  }
  
  setBounds() {
    const bounds = this.slides[0].getBoundingClientRect()
    const slideWidth = bounds.width

    this.sliderWidth = this.slidesNumb * slideWidth
    this.max = -(this.sliderWidth - window.innerWidth)
    
    this.slides.forEach((slide, index) => {
      slide.style.left = `${index * slideWidth}px`
    })
  }
  
  setPos(e) {
    if (!this.isDragging) return
    this.currentX = this.offX + ((e.clientX - this.onX) * this.opts.speed)
    this.clamp()
  }

  clamp() {
    this.currentX = Math.max(Math.min(this.currentX, this.min), this.max)
  }
  
  run() {
    this.lastX = lerp(this.lastX, this.currentX, this.opts.ease)
    this.lastX = Math.floor(this.lastX * 100) / 100 

    const sd = this.currentX - this.lastX
    const acc = sd / window.innerWidth
    let velo =+ acc
    
    this.sliderInner.style.transform = `translate3d(${this.lastX}px, 0, 0) skewX(${velo * this.opts.velocity}deg)`

    this.requestAnimationFrame()
  }
  
  on(e) {
    this.isDragging = true
    this.onX = e.clientX
    this.slider.classList.add('is-grabbing')
  }
  
  off(e) {
    this.snap()
    this.isDragging = false
    this.offX = this.currentX
    this.slider.classList.remove('is-grabbing')
  }
  
  closest() {
    const numbers = []
    this.slides.forEach((slide, index) => {
      const bounds = slide.getBoundingClientRect()
      const diff = this.currentX - this.lastX
      const center = (bounds.x + diff) + (bounds.width / 2)
      const fromCenter = this.centerX - center
      numbers.push(fromCenter)
    })

    let closest = number(0, numbers)
    closest = numbers[closest]
    
    return {
      closest
    }
  }

  snap() {
    const { closest } = this.closest()
    
    this.currentX = this.currentX + closest
    this.clamp()
  }

  requestAnimationFrame() {
    this.rAF = requestAnimationFrame(this.run)
  }

  cancelAnimationFrame() {
    cancelAnimationFrame(this.rAF)
  }
  
  addEvents() {
    this.run()
    
    this.slider.addEventListener('mousemove', this.setPos, { passive: true })
    this.slider.addEventListener('mousedown', this.on, false)
    this.slider.addEventListener('mouseup', this.off, false)
    
    window.addEventListener('resize', this.resize, false)
  }
  
  removeEvents() {
    this.cancelAnimationFrame(this.rAF)
    
    this.slider.removeEventListener('mousemove', this.setPos, { passive: true })
    this.slider.removeEventListener('mousedown', this.on, false)
    this.slider.removeEventListener('mouseup', this.off, false)
  }
  
  resize() {
    this.setBounds()
  }
  
  destroy() {
    this.removeEvents()
    
    this.opts = {}
  }
  
  init() {
    this.setBounds()
    this.addEvents()
  }
}

const slider = new Slider()
slider.init()
              
            
!
999px

Console