vue 列表的排序过渡 shuffle遇到的问题

内部的实现,Vue 使用了一个叫 FLIP 简单的动画队列
使用 transforms 将元素从之前的位置平滑过渡新的位置

需要注意的是使用 FLIP 过渡的元素不能设置为 display: inline 。作为替代方案,可以设置为 display: inline-block 或者放置于 flex 中

FLIP 动画不仅可以实现单列过渡,多维网格也同样可以过渡

<template>
    <div class="n-tag">
        <h1>所有标签</h1>
        <button @click="shuffle">shuffle</button>
        <transition-group name="cell" tag="div" class="container">
            <div v-for="cell in cells" :key="cell.id" class="cell">
                {{cell.number}}
            </div>
        </transition-group>
    </div>
</template>

<script>
// 要引入lodash.js
import _ from "lodash/lodash";
export default {
  data() {
    return {
      cells: Array.apply(null, {
        length: 81
      }).map(function(_, index) {
        return {
          id: index,
          number: index % 9 + 1
        };
      })
    };
  },
  methods: {
    shuffle() {
      this.cells = _.shuffle(this.cells);
    }
  }
};
</script>

<style scoped>
.n-tag {
  background: #fff;
  padding: 20px;
}
.container {
  display: flex;
  flex-wrap: wrap;
  width: 238px;
  margin-top: 10px;
}
.cell {
  display: flex;
  justify-content: space-around;
  align-items: center;
  width: 25px;
  height: 25px;
  border: 1px solid #aaa;
  margin-right: -1px;
  margin-bottom: -1px;
}
.cell:nth-child(3n) {
  margin-right: 0;
}
.cell:nth-child(27n) {
  margin-bottom: 0;
}
.cell-move {
  transition: transform 1s;
}
</style>

若没有引入lodash.js 会报错

修改.eslint.js 文件 ,增加属性

  "globals": {
    "document": true,
    "localStorage": true,
    "window": true,
    "_": true
  }

然后引用lodash.js

import _ from "lodash/lodash";
原文地址:https://www.cnblogs.com/shengnan-2017/p/9056208.html