3

我已经做了一些 vuejs 快一年了,但我从来没有真正让动画和过渡工作,我似乎很不清楚这一切应该如何工作,今天我决定尝试并最终理解它们,但我又一次卡住了,过渡根本行不通,我不明白我做错了什么。这是我的代码:

的HTML

<div id="app">
 <div class="row">
      <div class="container-fluid">
        <transition-group name="flip-list" tag="div" class="row horizontal-scroll">
          <div class="col-xs-12 col-md-2 col-lg-2 col-card" 
            v-for="(someCard, index) in someCards" v-bind:key="index">
            <div class="some-card" @click="changeOrder(someCard)">
              {{ someCard.someTitle }}
            </div>
          </div>
        </transition-group>
      </div>
    </div>
</div>

JS

new Vue({
  el: "#app",
  data: {
    someCards: [
          {
            order: 2,
            someTitle: 'Title 1'
          },
          {
            order: 2,
            someTitle: 'Title 2'
          },
          {
            order: 2,
            someTitle: 'Title 3'
          },
          {
            order: 3,
            someTitle: 'Title 4'
          },
          {
            order: 3,
            someTitle: 'Title 5'
          },
          {
            order: 1,
            someTitle: 'Title 6'
          },
          {
            order: 1,
            someTitle: 'Title 7'
          },
          {
            order: 1,
            someTitle: 'Title 8'
          },
          {
            order: 3,
            someTitle: 'Title 1'
          }
        ]
  },
  methods: {
    changeOrder(someCardData) {
        let newRandom = Math.floor(Math.random() * 3) + 1;
        while (newRandom === someCardData.order) {
          newRandom = Math.floor(Math.random() * 3) + 1;
        }
        someCardData.order = newRandom;
        this.reorderList();
      },
      reorderList() {
        this.someCards.sort(function(objA, objB) {
          return objA.order - objB.order;
        })
      },
  }
})

CSS

/* default */
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}
#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}
/* bootstrap */
.row {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  margin-right: -15px;
  margin-left: -15px;
}
.col-1, .col-2, .col-3, .col-4, .col-5, .col-6, .col-7, .col-8, .col-9, .col-10, .col-11, .col-12, .col, .col-auto, .col-sm-1, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6, .col-sm-7, .col-sm-8, .col-sm-9, .col-sm-10, .col-sm-11, .col-sm-12, .col-sm, .col-sm-auto, .col-md-1, .col-md-2, .col-md-3, .col-md-4, .col-md-5, .col-md-6, .col-md-7, .col-md-8, .col-md-9, .col-md-10, .col-md-11, .col-md-12, .col-md, .col-md-auto, .col-lg-1, .col-lg-2, .col-lg-3, .col-lg-4, .col-lg-5, .col-lg-6, .col-lg-7, .col-lg-8, .col-lg-9, .col-lg-10, .col-lg-11, .col-lg-12, .col-lg, .col-lg-auto, .col-xl-1, .col-xl-2, .col-xl-3, .col-xl-4, .col-xl-5, .col-xl-6, .col-xl-7, .col-xl-8, .col-xl-9, .col-xl-10, .col-xl-11, .col-xl-12, .col-xl, .col-xl-auto {
    position: relative;
    width: 100%;
    min-height: 1px;
    padding-right: 15px;
    padding-left: 15px;
}

/* my css */
.horizontal-scroll {
  overflow-x: auto;
  -ms-flex-wrap: nowrap;
  flex-wrap: nowrap;
}
.col-card {
  max-width: 14%;
}
.some-card {
  height: 220px;
  font-size: 16px;
  text-align: center;
  padding-right: 5px;
  margin-top: 5px;
  -webkit-box-shadow: 1px 1px 5px rgba(0,0,0,.1);
  box-shadow: 1px 1px 5px rgba(0,0,0,.1);
  border-radius: 2px;
  background: #fff;
  margin-bottom: 20px;
  position: relative;
  border-top-style: solid;
}
.flip-list-move {
  transition: transform 1s;
}

https://jsfiddle.net/eywraw8t/459088/

当我在连续对齐的卡片列表中重新排序卡片时,我试图触发动画。要更改卡片的位置,只需单击它。重新排序有效,但未完成转换。

我使用了这个文档 https://vuejs.org/v2/guide/transitions.html#List-Move-Transitions 并且看过一些相当简单的教程,但我一定遗漏了一些东西。有人可以检查代码并告诉我它是什么吗?

谢谢

4

1 回答 1

6

这是因为您使用索引作为键。即使卡片发生变化,相同位置的索引也将始终相同。我会给每张卡一个唯一的 id,然后用它作为密钥来代替..

new Vue({
  el: "#app",
  data: {
    someCards: [{
        id: 0,
        order: 2,
        someTitle: 'Title 1'
      },
      {
        id: 1,
        order: 2,
        someTitle: 'Title 2'
      },
      {
        id: 2,
        order: 2,
        someTitle: 'Title 3'
      },
      {
        id: 3,
        order: 3,
        someTitle: 'Title 4'
      },
      {
        id: 4,
        order: 3,
        someTitle: 'Title 5'
      },
      {
        id: 5,
        order: 1,
        someTitle: 'Title 6'
      },
      {
        id: 6,
        order: 1,
        someTitle: 'Title 7'
      },
      {
        id: 7,
        order: 1,
        someTitle: 'Title 8'
      },
      {
        id: 8,
        order: 3,
        someTitle: 'Title 1'
      }
    ]
  },
  methods: {
    changeOrder(someCardData) {
      let newRandom = Math.floor(Math.random() * 3) + 1;
      while (newRandom === someCardData.order) {
        newRandom = Math.floor(Math.random() * 3) + 1;
      }
      someCardData.order = newRandom;
      this.reorderList();
    },
    reorderList() {
      this.someCards.sort(function(objA, objB) {
        return objA.order - objB.order;
      })
    },
  }
})
/* default */

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}


/* bootstrap */

.row {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  margin-right: -15px;
  margin-left: -15px;
}

.col-1,
.col-2,
.col-3,
.col-4,
.col-5,
.col-6,
.col-7,
.col-8,
.col-9,
.col-10,
.col-11,
.col-12,
.col,
.col-auto,
.col-sm-1,
.col-sm-2,
.col-sm-3,
.col-sm-4,
.col-sm-5,
.col-sm-6,
.col-sm-7,
.col-sm-8,
.col-sm-9,
.col-sm-10,
.col-sm-11,
.col-sm-12,
.col-sm,
.col-sm-auto,
.col-md-1,
.col-md-2,
.col-md-3,
.col-md-4,
.col-md-5,
.col-md-6,
.col-md-7,
.col-md-8,
.col-md-9,
.col-md-10,
.col-md-11,
.col-md-12,
.col-md,
.col-md-auto,
.col-lg-1,
.col-lg-2,
.col-lg-3,
.col-lg-4,
.col-lg-5,
.col-lg-6,
.col-lg-7,
.col-lg-8,
.col-lg-9,
.col-lg-10,
.col-lg-11,
.col-lg-12,
.col-lg,
.col-lg-auto,
.col-xl-1,
.col-xl-2,
.col-xl-3,
.col-xl-4,
.col-xl-5,
.col-xl-6,
.col-xl-7,
.col-xl-8,
.col-xl-9,
.col-xl-10,
.col-xl-11,
.col-xl-12,
.col-xl,
.col-xl-auto {
  position: relative;
  width: 100%;
  min-height: 1px;
  padding-right: 15px;
  padding-left: 15px;
}


/* my css */

.horizontal-scroll {
  overflow-x: auto;
  -ms-flex-wrap: nowrap;
  flex-wrap: nowrap;
}

.col-card {
  max-width: 14%;
}

.some-card {
  height: 220px;
  font-size: 16px;
  text-align: center;
  padding-right: 5px;
  margin-top: 5px;
  -webkit-box-shadow: 1px 1px 5px rgba(0, 0, 0, .1);
  box-shadow: 1px 1px 5px rgba(0, 0, 0, .1);
  border-radius: 2px;
  background: #fff;
  margin-bottom: 20px;
  position: relative;
  border-top-style: solid;
}

.flip-list-move {
  transition: transform 1s;
}
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>

<div id="app">
  <div class="row">
    <div class="container-fluid">
      <transition-group name="flip-list" tag="div" class="row horizontal-scroll">
        <div class="col-xs-12 col-md-2 col-lg-2 col-card" v-for="(someCard, index) in someCards" v-bind:key="someCard.id">
          <div class="some-card" @click="changeOrder(someCard)">
            {{ someCard.someTitle }}
          </div>
        </div>
      </transition-group>
    </div>
  </div>
</div>

JSFiddle

于 2018-11-14T13:32:04.357 回答