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>
相关推荐
东方小月6 小时前
从零开发一个 Coding Agent(四):使用状态机校验大模型事件流
前端·人工智能·后端
想做后端的前端7 小时前
WebGL实现FPS游戏
vue.js·游戏·webgl
Csvn7 小时前
🧩 ESM vs CJS 混用的 7 个「天坑」——从 TypeScript 编译到 Node 与浏览器
前端
Csvn7 小时前
🎯 Web 性能 API 集合:Performance Observer 的 5 个冷门妙用
前端
Csvn7 小时前
深入 React 闭包陷阱:从根源上理解并根治 stale closure
前端
whyfail7 小时前
前端学 Spring Boot(8):接口为什么越用越慢?
前端·spring boot·后端
用户059540174468 小时前
LangChain 记忆测试踩坑实录:这两个坑让我排查了 4 小时
前端·css
程序员黑豆8 小时前
鸿蒙应用开发:@Monitor 装饰器使用教程
前端·harmonyos
用户938515635078 小时前
从零构建《天龙八部》知识库:EPUB 加载→文本分割→向量嵌入→Milvus 存储→RAG 问答,一条链路打通
javascript·人工智能·全栈
SamChan909 小时前
在Web应用中集成PDF多语言翻译功能:PDFTranslator API实战指南
前端·python·ai·pdf·yapi·机器翻译