Vue - 过渡 列表过渡

列表的进入/离开过渡

获取不大于数组长度的随机数,作为插入新值的位置

<div id="app" class="demo">
    <button @click="add">Add</button>
    <button @click="remove">Remove</button>
    <br>
    <br>
    <transition-group name="list">
        <span v-for="item in items" :key="item" class="list-item">{{item}}</span>
    </transition-group>
</div>
<script>
new Vue({
    el: '#app',
    data: {
        items: [1, 2, 3, 4, 5, 6, 7, 8, 9],
        num: 10
    },
    methods: {
        randomIndex () {
            // 获取不超过数组长度的随机数
            return Math.floor(Math.random() * this.items.length)
        },
        add () {
            // 把获取的随机数作为位置插入新元素
            this.items.splice(this.randomIndex(), 0, this.num++)
        },
        remove () {
            // 随机删除某个位置的元素
            this.items.splice(this.randomIndex(), 1)
        }
    }
})
</script>
<style>
.list-item{
    margin-right: 10px;
    display: inline-block;
}
.list-enter-active, .list-leave-active{
    transition: all 1s;
}
.list-enter, .list-leave-to{
    opacity: 0;
    transform: translateY(30px);
}
</style>

1321525-20180615064922251-305304408.gif

列表的排序过渡

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

<div id="app">
    <button @click="shuffle">Shuffle</button>
    <br>
    <br>
    <transition-group name="flip-list">
        <li v-for="item in items" :key="item">{{item}}</li>
    </transition-group>
</div>
<script>
new Vue({
    el: '#app',
    data: {
        items: [1, 2, 3, 4, 5, 6, 7, 8, 9]
    },
    methods: {
        shuffle () {
            this.items = _.shuffle(this.items);
        }
    }
})
</script>
<style>
.flip-list-move{
    transition: all .3s;
}
</style>

1321525-20180615070153385-1755716150.gif


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

我们将之前实现的例子和这个技术结合,使我们列表的一切变动都会有动画过渡。

<div id="app">
    <button @click="shuffle">Shuffle</button>
    <button @click="add">Add</button>
    <button @click="remove">Remove</button>
    <br>
    <br>
    <transition-group name="list">
        <span v-for="item in items" :key="item" class="list-item">{{item}}</span>
    </transition-group>
</div>
<script>
new Vue({
    el: '#app',
    data: {
        items: [1, 2, 3, 4, 5, 6, 7, 8, 9],
        num: 10
    },
    methods: {
        randomIndex () {
            // 获取不超过数组长度的随机数
            return Math.floor(Math.random() * this.items.length)
        },
        add () {
            // 把获取的随机数作为位置插入新元素
            this.items.splice(this.randomIndex(), 0, this.num++)
        },
        remove () {
            // 随机删除某个位置的元素
            this.items.splice(this.randomIndex(), 1)
        },
        shuffle () {
            this.items = _.shuffle(this.items)
        }
    }
})
</script>
<style>
.list-item{
    transition: all 1s;
    margin-right: 10px;
    display: inline-block;
}
.list-enter, .list-leave-to{
    opacity: 0;
    transform: translateY(30px);
}
.list-leave-active{
    position: absolute;
}
</style>

1321525-20180615072002969-901289647.gif


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

<div id="app">
    <button @click="shuffle">Shuffle</button>
    <br>
    <br>
    <transition-group name="cell" class="sudoku-container">
        <span v-for="cell in cells" :key="cell.id" class="cell">{{cell.number}}</span>
    </transition-group>
</div>
<script>
new Vue({
    el: '#app',
    data: {
        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>
.sudoku-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>

1321525-20180615071707309-1076148451.gif


转载于:https://www.cnblogs.com/xiaobaiv/p/9181240.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值