Vue中transition单个节点的过渡与transition-group列表过渡

transition 只能满足单个节点的过渡效果,在多个节点的渲染上显得力不从心

为了更好适用于更多的场景,vue 提供 <transition-ground>来多个元素的过渡

首先创建了一个简单的应用,通过button  来实现动态的加减,使用了啊你,实际效果可运行一下代码

[html]  view plain  copy
  1. <body>  
  2.        <div id="app">  
  3.        <button @click="add">add</button>  
  4.        <button @click="move">remove</button>  
  5.            <transition-group  
  6.            name="list"  
  7.            enter-active-class="animated bounceInDown"  
  8.            leave-active-class="animated bounceOut"  
  9.            >  
  10.                <li v-for="item in num" :key="item">{{item}}</li>  
  11.            </transition-group>  
  12.        </div>  
  13.   
  14.    </body>  
  15.    <script>  
  16.   
  17.        var app=new Vue({  
  18.            el:"#app",  
  19.            data:{  
  20.                show:true,  
  21.                num:10,  
  22.                statu:false  
  23.            },  
  24.            methods:{  
  25.                add:function(){  
  26.                      this.num++;  
  27.                    },  
  28.                move:function(){  
  29.                    this.num--;  
  30.                }  
  31.            }  
  32.        });  
  33.   
  34.    </script>  

transition-group 拥有transition所有属性

但是需要关注的是它们的不同之处:

transition本身不会渲染出元素

但是transition-group 会渲染出元素节点;默认  tag属性为<span>,可修改。

ps:transition-group 的元素必须指定key 属性


在列表元素的动态加入移除中,整个列表会因为元素的改变而相应的改变位子,这些位子属性的改变会很生硬,所以transition-group给出prop  move-class;支持通过 CSS transform 过渡移动。当一个子节点被更新,从屏幕上的位置发生变化,它将会获取应用 CSS 移动类

[html]  view plain  copy
  1.       <style>  
  2.         .div1{  
  3.             width:156px;  
  4.         }  
  5.         .cla1{  
  6.             border:1px solid #222;  
  7.             display:inline-block;  
  8.             width:50px;  
  9.             height:50px;  
  10.             text-align: center;  
  11.             vertical-align: middle;  
  12.         }  
  13.             .flip-list-move {  
  14.   transition: transform 1s;  
  15. }  
  16.         </style>  
  17.     </head>  
  18.     <body>  
  19.         <div id="app">  
  20.         <button @click="chang">reverse</button>  
  21.         <transition-group tag="div" class="div1" name="flip-list">  
  22.             <span class="cla1" v-for="item in list" :key="item">{{item}}</span>  
  23.         </transition-group>  
  24.   
  25.         <button @click="add">add</button>  
  26.         <button @click="move">remove</button>  
  27.             <transition-group  
  28.             tag="ul"  
  29.             name="flip-list"  
  30.             enter-active-class="animated bounceInDown"  
  31.             leave-active-class="animated bounceOut"  
  32.             >  
  33.                 <li v-for="(item,index) in num" :key="item">{{item}}={{index}}</li>  
  34.             </transition-group>  
  35.         </div>  
  36.   
  37.     </body>  
  38.     <script>  
  39.     function shuffle(arr){  
  40.     var result = [],  
  41.         random;  
  42.     while(arr.length>0){  
  43.         random = Math.floor(Math.random() * arr.length);  
  44.         result.push(arr[random])  
  45.         arr.splice(random, 1)  
  46.     }  
  47.     return result;  
  48. }  
  49.         var app=new Vue({  
  50.             el:"#app",  
  51.             data:{  
  52.                 show:true,  
  53.                 n:3,  
  54.                 num:[1,2,3],  
  55.                 list:[1,2,3,4,5,6,7,8,9],  
  56.                 statu:false  
  57.             },  
  58.             methods:{  
  59.                 chang:function(){  
  60.                     this.list=shuffle(app.list);  
  61.                 },  
  62.                 add:function(){  
  63.                       this.num.splice(3,0,++this.n)  
  64.                     },  
  65.                 move:function(){  
  66.                      this.num.splice(3,1)  
  67.                      console.log(this.num)  
  68.                 }  
  69.             }  
  70.         });  
  71.   
  72.     </script>  

猜你喜欢

转载自blog.csdn.net/weixin_38098192/article/details/80652258