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. 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

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

              
                <div id="app">
    <button @click="shuffle">shuffle</button>
    <hr>
    <div class="box">
      <div class="cell" ref="cell" v-for="(item) in list" :key="item">{{ item }}</div>
    </div>
  </div>
              
            
!

CSS

              
                .box {
  width: 500px;
}

.cell {
  box-sizing: border-box;
  float: left;
  width: 50px;
  height: 50px;
  line-height: 48px;
  text-align: center;
  border: 1px solid #000;
}
              
            
!

JS

              
                function shuffle(array) {
  let len = array.length;
  for (let i = len - 1; i > 0; i--) {
    let j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
  return array.slice()
}

function getPositionList(domList) {
  return domList.map((dom) => {
    const {left, top} = dom.getBoundingClientRect()
    return {left, top}
  })
}


var vm = new Vue({
  el:"#app",
  data() {
    let list = []
    for (let i = 0; i < 100; ++i) {
      list.push(i)
    }
    return {
      list
    }
  },

  methods: {
    shuffle() {
      this.list = shuffle(this.list)

      const cellList = this.$refs.cell.slice() // 保存快照

      const originPositions = getPositionList(cellList)

      this.$nextTick(() => {
        // 需要保证变化前后的DOM节点一致,因此v-for的key不能使用index
        const currentPositions = getPositionList(cellList)

        cellList.forEach((cell, index) => {
          let cur = currentPositions[index]
          let origin = originPositions[index]

          const invert = {
            left: origin.left - cur.left,
            top: origin.top - cur.top,
          }

          const keyframes = [
            {
              transform: `translate(${invert.left}px, ${invert.top}px)`,
            },
            {transform: "translate(0)"},
          ]

          const options = {
            duration: 3000,
            easing: "linear",
          }

          cell.animate(keyframes, options)
        })
      })


    }
  }
})
              
            
!
999px

Console