单击“ VUE 3中的过渡组”之后显示hide div

发布于 2025-02-09 03:00:21 字数 1034 浏览 2 评论 0原文

显示新元素后

在单击旧元素隐藏使用动画之后,我需要使用动画模板

<div id="app">
 
  
      <transition-group name="list">
        <div style="cursor:pointer" v-for="(item,key,i) in array" :key="i" class="list-item" >
          <div @click="select(key)">{{key}}</div>
        </div>
      </transition-group>
   
  <div>
   {{element}}
  </div>
</div>

:脚本

export default {
  name: 'App',
 data() {
    return {
    element:"hello",
    array: ["hello","hello1","hello2"],
    }
  
  },
   methods:{
     select(key){
       this.element = this.array[key]
     }
   },
  components: {
    HelloWorld
  }
}

.list-item {
  transition: all 1s;
}
.list-leave-active {
  display: none;
}
.list-enter {
  opacity: 0;
}

https://stackblitz.com/edit/vue-tr9ucq?file=src/app.vue

I need that after clicking old element hide with animation and a new one also show with animation

Template:

<div id="app">
 
  
      <transition-group name="list">
        <div style="cursor:pointer" v-for="(item,key,i) in array" :key="i" class="list-item" >
          <div @click="select(key)">{{key}}</div>
        </div>
      </transition-group>
   
  <div>
   {{element}}
  </div>
</div>

Script:

export default {
  name: 'App',
 data() {
    return {
    element:"hello",
    array: ["hello","hello1","hello2"],
    }
  
  },
   methods:{
     select(key){
       this.element = this.array[key]
     }
   },
  components: {
    HelloWorld
  }
}

Style:

.list-item {
  transition: all 1s;
}
.list-leave-active {
  display: none;
}
.list-enter {
  opacity: 0;
}

https://stackblitz.com/edit/vue-tr9ucq?file=src/App.vue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言 2025-02-16 03:00:21

请参阅此snippet 受VUE官方doc https://v.vuejs.org/v2/guide/guide/transitions.html

更新列表(添加/删除项目)以触发Vue Transition的逻辑。

 <transition-group name="list" tag="div" class="list">
        <span
          v-for="(item, index) in array"
          v-bind:key="item"
          @click="remove(index)"
          class="list-item"
        >
          {{ item }}
        </span>
      </transition-group>

通过点击事件将索引传递,然后执行逻辑以更新您的数组

methods: {
    remove(index) {
      this.removed.push(this.array[index]);
      this.array.splice(index, 1);
    },
  },


See this snippet inspired by Vue official doc https://v2.vuejs.org/v2/guide/transitions.html

You were missing the logic to update your list (add/remove items) to trigger vue transition.

 <transition-group name="list" tag="div" class="list">
        <span
          v-for="(item, index) in array"
          v-bind:key="item"
          @click="remove(index)"
          class="list-item"
        >
          {{ item }}
        </span>
      </transition-group>

Pass the index through click event then do your logic to update your array

methods: {
    remove(index) {
      this.removed.push(this.array[index]);
      this.array.splice(index, 1);
    },
  },


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文