Web前端vue必做笔记之一:列表动画(vue小技巧)

这篇博客探讨了在Web前端开发中,如何利用Vue的transition-group组件实现列表动画效果,是Vue.js开发者必学的一项小技巧。

Web前端vue必做笔记之一:列表动画(vue小技巧)

<template>
     <div id="app">
         <!-- list-enter-active  当我们操作列表中元素时,他会给我们加入这样的类名--> 
          <transition-group tag="ul" name="list">
                <li class="item"  @click="deleteItem(item)"  v-for="item in list" :key="item.id">
                    {{item.title}}
                </li>
          </transition-group>

          <br/>
          <button @click="addItem">增加一项</button>
     </div>
</template>

<script>
export default {
     data(){
         return {
               list:[
                 {
                     id:1,
                     title:'项目',
                 },
                                  {
                     id:1,
                     title:'项目',
                 },
                                  {
                     id:1,
                     title:'项目',
                 },
               ]
         }
     },
     methods:{
        //   删除一项
        deleteItem(item){
              const index = this.list.findIndex((i) =>{
                   return i.id === item.id;
              });
              this.list.splice(index, 1);
        },
        // 增加一项
        addItem(){
              const index = Math.random();
              const item = {
                   id:index,
                   title:`项目${index}`,
              };
        }
     }
}
</script>
<style>
   ul{
       padding: 0;
       margin: 0;
       list-style: none;
   }

   item {
         height: 40px;
         border: 1px solid #000;
   }

   .list-enter-active,
   .list-leave-active{
        transition: height 0.1s linear;
   }
    
    /* 进场:height 0 ->40;
    离场:height 40 ->0; */
   .list-enter,
   .list-leave-to {
       height: 0;
   }
</style>

这里主要使用了transition-group 这个组件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值