grid布局下的展开/收缩过渡效果【vue/已验证可正常运行】

代码来自GPT4o:国内官方直连GPT4o

javascript 复制代码
<template>
  <div class="container">
    <button class="butns" @click="toggleShowMore">{{ showAll ? '收回' : '显示更多' }}</button>
    <transition-group name="slide-fade" tag="div" class="grid">
      <div v-for="(item, index) in displayedItems" :key="item" class="grid-item">
        {{ item }}
      </div>
    </transition-group>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6', 'Item 7', 'Item 8', 'Item 9'],
      showAll: false
    };
  },
  computed: {
    displayedItems() {
      return this.showAll ? this.items : this.items.slice(0, 6);
    }
  },
  methods: {
    toggleShowMore() {
      this.showAll = !this.showAll;
    }
  }
};
</script>

<style scoped>
.butns {
    position: absolute;
    top: 100px;
    left: 0px;
}
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  overflow: hidden;
}

.grid-item {
  background-color: #f0f0f0;
  padding: 20px;
  text-align: center;
  border: 1px solid #ccc;
}

button {
  margin-top: 20px;
  padding: 10px 20px;
  font-size: 16px;
  cursor: pointer;
}

.slide-fade-enter-active, .slide-fade-leave-active {
  transition: all 0.5s ease;
}

.slide-fade-enter, .slide-fade-leave-to {
  max-height: 0;
  opacity: 0;
  margin: 0;
  padding: 0;
}

.slide-fade-enter-to, .slide-fade-leave {
  max-height: 200px; /* Adjust based on your content's height */
  opacity: 1;
}
</style>
相关推荐
liulilittle3 分钟前
C++ 浮点数封装。
linux·服务器·开发语言·前端·网络·数据库·c++
wordbaby15 分钟前
Expo 进阶指南:赋予 TanStack Query “原生感知力” —— 深度解析 AppState 与 NetInfo
前端·react native
Moment21 分钟前
从美团全栈化看 AI 冲击:前端转全栈,是自救还是必然 🤔🤔🤔
前端·后端·面试
天问一29 分钟前
使用 Vue Router 进行路由定制和调用的示例
前端·javascript·vue.js
源码获取_wx:Fegn08952 小时前
基于springboot + vue心理健康管理系统
vue.js·spring boot·后端
韩立学长2 小时前
【开题答辩实录分享】以《基于Vue的非遗文化知识分享平台的设计与实现》为例进行选题答辩实录分享
前端·javascript·vue.js
优弧2 小时前
离开舒适区100天,我后悔了吗?
前端·后端·面试
胡gh2 小时前
css的臂膀,前端动效的利器,还是布局的“隐形陷阱”?
前端·css·html
灵感菇_2 小时前
Flutter Riverpod 完整教程:从入门到实战
前端·flutter·ui·状态管理
用户21411832636022 小时前
紧急修复!Dify CVE-2025-55182 高危漏洞,手把手教你升级避坑
前端