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

在这里插入图片描述
代码来自GPT4o:国内官方直连GPT4o

<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>

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 05:28:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 05:28:01       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 05:28:01       58 阅读
  4. Python语言-面向对象

    2024-07-11 05:28:01       69 阅读

热门阅读

  1. 从像素角度出发使用OpenCV检测图像是否为彩色

    2024-07-11 05:28:01       27 阅读
  2. ES索引模板

    2024-07-11 05:28:01       20 阅读
  3. ”极大似然估计“和”贝叶斯估计“思想对比

    2024-07-11 05:28:01       24 阅读
  4. 理解Gunicorn:Python WSGI服务器的基石

    2024-07-11 05:28:01       24 阅读
  5. C++函数模板学习

    2024-07-11 05:28:01       19 阅读
  6. 探索Perl的自动清洁工:垃圾收集机制全解析

    2024-07-11 05:28:01       21 阅读
  7. Kruskal

    2024-07-11 05:28:01       23 阅读
  8. C++入门

    C++入门

    2024-07-11 05:28:01      21 阅读
  9. Spring框架配置进阶_自动装配(XML和注解)

    2024-07-11 05:28:01       21 阅读