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>
相关推荐
wuhen_n1 小时前
网络请求在Vite层的代理与Mock:告别跨域和后端依赖
前端·javascript·vue.js
小彭努力中1 小时前
193.Vue3 + OpenLayers 实战:圆孔相机模型推算卫星拍摄区域
vue.js·数码相机·vue·openlayers·geojson
用户69371750013846 小时前
Google 正在“收紧侧加载”:陌生 APK 安装或需等待 24 小时
android·前端
蓝帆傲亦6 小时前
Web 前端搜索文字高亮实现方法汇总
前端
用户69371750013846 小时前
Room 3.0:这次不是升级,是重来
android·前端·google
漫随流水8 小时前
旅游推荐系统(view.py)
前端·数据库·python·旅游
踩着两条虫9 小时前
VTJ.PRO 核心架构全公开!从设计稿到代码,揭秘AI智能体如何“听懂人话”
前端·vue.js·ai编程
jzlhll12310 小时前
kotlin Flow first() last()总结
开发语言·前端·kotlin
用头发抵命10 小时前
Vue 3 中优雅地集成 Video.js 播放器:从组件封装到功能定制
开发语言·javascript·ecmascript
蓝冰凌10 小时前
Vue 3 中 defineExpose 的行为【defineExpose暴露ref变量】详解:自动解包、响应性与实际使用
前端·javascript·vue.js