使用v-move类时,是否不触发Vue <transition-group>?

问题描述

我一直在努力使<transition-group>正常工作,但我只是想不通。单击该框后,该元素将更改其位置-假定触发v-move类。 This是我正在查看的Vue文档的特定部分。

Here is the link to my test app

<style>

.test-group-leave-to{
    opacity: 0;
}
.test-group-move{
    transition: transform 5s;
}

</style>

<body>

<div id='app'>
    <test-comp></test-comp>
</div>
<script>

let arrary = ['1','2','3','4','5','6','7','8','9'];

new Vue({
    el: "#app",components: {
        "test-comp":{
            template: `
                <div style='display:inline-block;'>
                    <transition-group name='test-group'>
                        <div 
                        class='Box' 
                        v-for='(all,ind) in arr' 
                        :key='all' 
                        @click='del(ind,$event)'>{{ all }}</div>
                    </transition-group>
                </div>
            `,data(){
                return {
                    arr: arrary
                }
            },methods: {
                del(ind,e){
                    e.target.style.left = '100px';
                    this.arr.splice(ind,1);
                }
            }
        }
    }
});
</script>

解决方法

为了获得更流畅的动画有些不同的方法:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>

<meta name="viewport" content="width=device-width,initial-scale=1.0">

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.19/lodash.min.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Balsamiq+Sans:wght@400;700&family=Indie+Flower&family=Nunito+Sans:wght@400;600;700;800;900&display=swap" rel="stylesheet">

<style>

body{
    position: relative;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    font-family: 'Nunito',sans-serif;
}
.outer-box {
    position: relative;
    width: 100%;
    padding: 5px 10px;
    transition: all 1s;
}
.box{
    border: 1px solid blue;
    text-align: center;
    width: 50px;
    height: 50px;
    user-select: none;  
}

.animate_move-enter,.animate_move-leave-to {
  opacity: 0;
  transform: translateX(100px);
}
.animate_move-leave-active {
  position: absolute;
}

</style>
</head>
<body>

<div id='app'>
    <test-comp></test-comp>
</div>
<script>

let arrary = ['1','2','3','4','5','6','7','8','9'];

new Vue({
    el: "#app",components: {
        "test-comp":{
            template: `
                <div>
                    <transition-group name='animate_move'>
                      <div v-for='(all,ind) in arr' :key='all' class="outer-box">
                          <div class='box' @click='del(ind,$event)'>{{ all }}</div>
                      </div>
                    </transition-group>
                </div>
            `,data(){
                return {
                    arr: arrary
                }
            },methods: {
                del(ind,e){
                    this.arr.splice(ind,1);
                }
            }
        }
    }
});
</script>
</body>
</html>


通过这种方式,您可以避免在块被删除之后跳转。

,

我建议使用List Entering/Leaving Transitions而不是移动过渡,您应该删除e.target.style.left = '100px';

<!DOCTYPE html>
<html>

<head>
  <title>Page Title</title>

  <meta name="viewport" content="width=device-width,initial-scale=1.0">

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.19/lodash.min.js"></script>
  <link href="https://fonts.googleapis.com/css2?family=Balsamiq+Sans:wght@400;700&family=Indie+Flower&family=Nunito+Sans:wght@400;600;700;800;900&display=swap" rel="stylesheet">

  <style>
    body {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      margin: 0;
      font-family: 'Nunito',sans-serif;
    }
    
    .box {
      position: relative;
      border: 1px solid blue;
      margin: 10px;
      text-align: center;
      width: 50px;
      height: 50px;
      user-select: none;
    }
    
    .test-group-enter-active,.test-group-leave-active {
      transition: all 1s;
    }
    
    .test-group-enter,.test-group-leave-to {
      opacity: 0;
      transform: translateX(100px);
    }
  </style>
</head>

<body>

  <div id='app'>
    <test-comp></test-comp>
  </div>
  <script>
    let arrary = ['1','9'];

    new Vue({
      el: "#app",components: {
        "test-comp": {
          template: `
                <div style='display:inline-block;'>
                    <transition-group name='test-group'>
                        <div 
                        class='box' 
                        v-for='(all,ind) in arr' 
                        :key='all' 
                        @click='del(ind,$event)'>{{ all }}</div>
                    </transition-group>
                </div>
            `,data() {
            return {
              arr: arrary
            }
          },methods: {
            del(ind,e) {

              //e.target.style.left = '100px';
              this.arr.splice(ind,1);
            }
          }
        }
      }
    });
  </script>
</body>

</html>