css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              .title Sortable List

- var n = 1;
.list(sortable-list)
  while n <= 10
    .list__item(sortable-item)
      .list__item-content
        .list__item-title= 'List Item ' + n++
        .list__item-description Aenean aliquam molestie urna, vel aliquam.
      .list__item-handle(sortable-handle)
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Montserrat:500')

body
  margin: 25px
  font-family: 'Montserrat', sans-serif
  font-weight: 500
  background: #e0e1e1
  -webkit-font-smoothing: antialiased
  -moz-osx-font-smoothing: grayscale
  -webkit-tap-highlight-color: transparent

*
  box-sizing: border-box

.title
  font-weight: 500
  text-align: center
  margin-bottom: 20px
  font-size: 20px
  color: #0E254E

.list
  margin: 0 auto
  width: 100%
  max-width: 380px
  user-select: none

  &__item
    transition: box-shadow 200ms ease-out, opacity 200ms ease-out
    border-radius: 6px
    background: #fff
    box-shadow: 0 0 12px rgba(0,0,0,0.05)
    display: flex

    &:not(:last-child)
      margin-bottom: 7px
    
    &.is-dragging
      box-shadow: 0 0 24px rgba(0,0,0,0.1)
      opacity: 0.8
    
    &-content
      width: calc(100% - 40px)
      padding: 10px 15px
    
    &-title,
    &-description
      white-space: nowrap
      text-overflow: ellipsis
      overflow: hidden
    
    &-title
      font-size: 15px
      color: #0E254E
    
    &-description
      font-size: 13px
      color: #66748E
    
    &-handle
      position: relative
      width: 40px
      cursor: pointer

      &:before,
      &:after
        content: ''
        position: absolute
        left: 15px
        right: 15px
        top: 50%
        height: 1px
        background: #c2cada
      
      &:before
        transform: translateY(-4px)
      
      &:after
        transform: translateY(4px)
            
          
!
            
              window.addEventListener('touchmove', () => {});

class Sortable {

  constructor(list, options) {
    this.list = (typeof list === 'string')
      ? document.querySelector(list)
      : list

    this.items = Array.from(this.list.children)
    this.animation = false

    this.options = Object.assign({
      animationSpeed: 200,
      animationEasing: 'ease-out',
    }, options || {})

    this.dragStart = this.dragStart.bind(this)
    this.dragMove = this.dragMove.bind(this)
    this.dragEnd = this.dragEnd.bind(this)

    this.list.addEventListener('touchstart', this.dragStart, false)
    this.list.addEventListener('mousedown', this.dragStart, false)
  }

  dragStart(e) {
    if (this.animation) return
    if (e.type === 'mousedown' && e.which !== 1) return
    if (e.type === 'touchstart' && e.touches.length > 1) return

    this.handle = null

    let el = e.target;
    while (el) {
      if (el.hasAttribute('sortable-handle')) this.handle = el
      if (el.hasAttribute('sortable-item')) this.item = el
      if (el.hasAttribute('sortable-list')) break;
      el = el.parentElement;
    }

    if (!this.handle) return

    this.list.style.position = 'relative'
    this.list.style.height = this.list.offsetHeight + 'px'

    this.item.classList.add('is-dragging')

    this.itemHeight = this.items[1].offsetTop
    this.listHeight = this.list.offsetHeight
    this.startTouchY = this.getDragY(e)
    this.startTop = this.item.offsetTop

    const offsetsTop = this.items.map(item => item.offsetTop)

    this.items.forEach((item, index) => {
      item.style.position = 'absolute'
      item.style.top = 0
      item.style.left = 0
      item.style.width = '100%'
      item.style.transform = `translateY(${offsetsTop[index]}px)`
      item.style.zIndex = (item == this.item) ? 2 : 1
    })

    setTimeout(() => {
      this.items.forEach(item => {
        if (this.item == item) return
        item.style.transition = `transform ${this.options.animationSpeed}ms ${this.options.animationEasing}`
      })
    })

    this.positions = this.items.map((item, index) => index)
    this.position = Math.round((this.startTop / this.listHeight) * this.items.length)

    this.touch = e.type == 'touchstart'
    window.addEventListener((this.touch ? 'touchmove' : 'mousemove'), this.dragMove, { passive: false })
    window.addEventListener((this.touch ? 'touchend' : 'mouseup'), this.dragEnd, false)
  }

  dragMove(e) {
    if (this.animation) return

    const top = this.startTop + this.getDragY(e) - this.startTouchY
    const newPosition = Math.round((top / this.listHeight) * this.items.length)

    this.item.style.transform = `translateY(${top}px)`

    this.positions.forEach(index => {
      if (index == this.position || index != newPosition) return
      this.swapElements(this.positions, this.position, index)
      this.position = index
    })

    this.items.forEach((item, index) => {
      if (item == this.item) return
      item.style.transform = `translateY(${this.positions.indexOf(index) * this.itemHeight}px)`
    })

    e.preventDefault()
  }

  dragEnd(e) {
    this.animation = true

    this.item.style.transition = `all ${this.options.animationSpeed}ms ${this.options.animationEasing}`
    this.item.style.transform = `translateY(${this.position * this.itemHeight}px)`

    this.item.classList.remove('is-dragging')

    setTimeout(() => {
      this.list.style.position = ''
      this.list.style.height = ''

      this.items.forEach(item => {
        item.style.top = ''
        item.style.left = ''
        item.style.right = ''
        item.style.position = ''
        item.style.transform = ''
        item.style.transition = ''
        item.style.width = ''
        item.style.zIndex = ''
      })

      this.positions.map(i => this.list.appendChild(this.items[i]))
      this.items = Array.from(this.list.children)

      this.animation = false
    }, this.options.animationSpeed)

    window.removeEventListener((this.touch ? 'touchmove' : 'mousemove'), this.dragMove, { passive: false })
    window.removeEventListener((this.touch ? 'touchend' : 'mouseup'), this.dragEnd, false)
  }

  swapElements(array, a, b) {
    const temp = array[a]
    array[a] = array[b]
    array[b] = temp
  }

  getDragY(e) {
    return e.touches ? (e.touches[0] || e.changedTouches[0]).pageY : e.pageY
  }

}

const sortable = new Sortable('.list')
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console