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>
相关推荐
森蓝情丶20 分钟前
我给 AI 搭了个法庭:一个前端仔的 LangGraph 实战全记录
前端·后端
爱勇宝23 分钟前
干了近 8 年,一夜之间被裁:AI 时代,程序员最该害怕的不是 AI
前端·后端·程序员
Pedantic33 分钟前
Combine 框架学习笔记
前端
runnerdancer1 小时前
Agent如何加载执行Skill的脚本
前端·agent
yingyima1 小时前
VS Code 正则替换技巧:从凌晨3点的服务器报警开始
前端
默_笙1 小时前
🛬 我让 AI 帮我写了一个打飞机游戏,结果 Canvas 把我整不会了
前端·javascript
梯度不陡1 小时前
AI 到底能不能从零写软件?ProgramBench 和 RepoZero 给出了两种答案
前端·javascript·面试
冬奇Lab2 小时前
每日一个开源项目(第137篇):Penpot - 真正开源的设计协作工具,SVG 原生格式消灭设计-开发鸿沟
前端·开源·设计
nuIl2 小时前
实现一个 Coding Agent(7):Skills
前端·agent·cursor
nuIl2 小时前
实现一个 Coding Agent(8):会话持久化与多会话
前端·agent·cursor