Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于Vue2.0的过渡效果与过渡状态--列表过渡 #5

Open
bluezhan opened this issue Nov 12, 2016 · 0 comments
Open

关于Vue2.0的过渡效果与过渡状态--列表过渡 #5

bluezhan opened this issue Nov 12, 2016 · 0 comments

Comments

@bluezhan
Copy link
Owner

bluezhan commented Nov 12, 2016

前言

在上一篇过渡效果 #2 里面,基本是提到了简单的,单个的组件的动画效果;Vue2.0上的列表过渡说的很清楚,只不过去理清楚之外肯定会思考例子和自己实践中得到不一样的知识。

列表过渡

目前为止,关于过渡我们已经讲到:

  • 单个节点

  • 一次渲染多个节点
    那么怎么同时渲染整个列表,比如使用 v-for ?在这种场景中,使用 <transition-group> 组件。在我们深入例子之前,先了解关于这个组件的几个特点:

  • 不同于 <transition>, 它会以一个真实元素呈现:默认为一个 <span>。你也可以通过 tag 特性更换为其他元素。

  • 元素 一定需要 指定唯一的 key 特性值

列表的进入和离开过渡

<div id="list-demo" class="demo">
  <button v-on:click="add">Add</button>
  <button v-on:click="remove">Remove</button>
  <transition-group name="list" tag="p">
    <span v-for="item in items" v-bind:key="item" class="list-item">
      {{ item }}
    </span>
  </transition-group>
</div>

这个例子有个问题,当添加和移除元素的时候,周围的元素会瞬间移动到他们的新布局的位置,而不是平滑的过渡,我们下面会解决这个问题。

列表的位移过渡

<transition-group> 组件还有一个特殊之处。不仅可以进入和离开动画,还可以改变定位。要使用这个新功能只需了解新增的 v-move 特性,它会在元素的改变定位的过程中应用。像之前的类名一样,可以通过 name 属性来自定义前缀,也可以通过 move-class 属性手动设置。

.flip-list-move {
  transition: transform 1s;
}

看上面这两家结合案例

官方装逼:

这个看起来很神奇,内部的实现,Vue 使用了一个叫 FLIP 简单的动画队列使用 transforms 将元素从之前的位置平滑过渡新的位置。我们将之前实现的例子和这个技术结合,使我们列表的一切变动都会有动画过渡。

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.14.1/lodash.min.js"></script>
<div id="list-complete-demo" class="demo">
  <button v-on:click="shuffle">Shuffle</button>
  <button v-on:click="add">Add</button>
  <button v-on:click="remove">Remove</button>
  <transition-group name="list-complete" tag="p">
    <span
      v-for="item in items"
      v-bind:key="item"
      class="list-complete-item"
    >
      {{ item }}
    </span>
  </transition-group>
</div>
.list-complete-item {
  transition: all 1s;
  display: inline-block;
  margin-right: 10px;
}
.list-complete-enter, .list-complete-leave-active {
  opacity: 0;
  transform: translateY(30px);
}
.list-complete-leave-active {
  position: absolute;
}
new Vue({
  el: '#list-complete-demo',
  data: {
    items: [1,2,3,4,5,6,7,8,9],
    nextNum: 10
  },
  methods: {
    randomIndex: function () {
      return Math.floor(Math.random() * this.items.length)
    },
    add: function () {
      this.items.splice(this.randomIndex(), 0, this.nextNum++)
    },
    remove: function () {
      this.items.splice(this.randomIndex(), 1)
    },
    shuffle: function () {
      this.items = _.shuffle(this.items)
    }
  }
})

就会发现其实啥也没弄,就是控制了CSS的类的加载和处理CSS3.0的动画的和位置。

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

延伸到案例中

第一个就是多维度的排列和连连看啊,队列里面的动画,再次就是匹配不匹配中来的动画。
就是所谓的有增删移动的东西都可以考虑这样的使用。

结合之前的动画的绑定中去处理。

可复用的过渡

动态过渡

三个库

到目前为止,看到使用了三个外库:

animate
velocity
lodash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant