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>
相关推荐
qq_3813385012 小时前
CSS @layer 级联层实战指南:从样式冲突到分层架构
前端·css
广州华水科技12 小时前
深度测评2026年好用的单北斗GNSS变形监测系统推荐,提升GNSS位移监测精度,引领智能监控新风尚
前端
花归去12 小时前
vue3中 function getText(){} 、 const getText=()=>{} ;区别在哪里,优缺点
javascript·vue.js·ecmascript
ZC跨境爬虫12 小时前
跟着 MDN 学 HTML day_33:(Attr 接口与属性节点的深入理解)
前端·javascript·ui·html·音视频·html5
神所夸赞的夏天12 小时前
如何获取多层json数据,存成dictionary,并取最大最小值
java·前端·json
红色的小鳄鱼12 小时前
前端面试js手写
开发语言·前端·javascript
焦糖玛奇朵婷12 小时前
健身房预约小程序开发、设计
java·大数据·服务器·前端·小程序
上海云盾王帅13 小时前
WEB业务如何接入安全防护:从零到一的实战指南
前端·安全
用户0595401744613 小时前
AI Agent记忆丢失踩坑实录:这个问题让我排查了3天
前端·css
web行路人13 小时前
前端对Commands(斜杠命令)一些常用
前端·javascript·vue.js·vue