当前位置:首页 > VUE

vue实现图片堆叠伸展

2026-02-24 09:15:41VUE

实现图片堆叠伸展效果

在Vue中实现图片堆叠伸展效果可以通过CSS和Vue的响应式特性结合完成。以下是具体实现方法:

使用CSS transform和transition

通过CSS的transform属性实现堆叠效果,transition实现平滑动画:

vue实现图片堆叠伸展

<template>
  <div class="stack-container">
    <div 
      v-for="(image, index) in images" 
      :key="index"
      :class="['stack-item', { active: activeIndex === index }]"
      @click="setActive(index)"
    >
      <img :src="image.src" :alt="image.alt">
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeIndex: 0,
      images: [
        { src: 'image1.jpg', alt: 'Image 1' },
        { src: 'image2.jpg', alt: 'Image 2' },
        { src: 'image3.jpg', alt: 'Image 3' }
      ]
    }
  },
  methods: {
    setActive(index) {
      this.activeIndex = index
    }
  }
}
</script>

<style>
.stack-container {
  position: relative;
  width: 300px;
  height: 200px;
}

.stack-item {
  position: absolute;
  width: 100%;
  height: 100%;
  transition: transform 0.3s ease;
  transform-origin: center bottom;
}

.stack-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.stack-item:not(.active) {
  transform: scale(0.9) translateY(10px);
  z-index: 1;
  filter: brightness(0.8);
}

.stack-item.active {
  transform: scale(1);
  z-index: 2;
}
</style>

使用Vue Transition组件

结合Vue的Transition组件实现更复杂的动画效果:

vue实现图片堆叠伸展

<template>
  <div class="stack-gallery">
    <TransitionGroup name="stack" tag="div" class="stack-wrapper">
      <div 
        v-for="(img, i) in images" 
        :key="img.id"
        :class="['stack-card', { 'active': activeCard === i }]"
        @click="activeCard = i"
      >
        <img :src="img.url" :alt="img.title">
      </div>
    </TransitionGroup>
  </div>
</template>

<style>
.stack-gallery {
  perspective: 1000px;
}

.stack-wrapper {
  position: relative;
  width: 300px;
  height: 400px;
}

.stack-card {
  position: absolute;
  width: 100%;
  height: 100%;
  transition: all 0.5s cubic-bezier(0.22, 1, 0.36, 1);
  transform-origin: center bottom;
}

.stack-card:not(.active) {
  transform: scale(0.85) translateY(30px);
  z-index: 1;
  opacity: 0.8;
}

.stack-card.active {
  transform: scale(1);
  z-index: 10;
}

.stack-enter-active,
.stack-leave-active {
  transition: all 0.5s ease;
}

.stack-enter-from,
.stack-leave-to {
  opacity: 0;
  transform: translateY(20px) scale(0.95);
}
</style>

响应式堆叠布局

实现根据屏幕大小调整堆叠数量的响应式效果:

computed: {
  visibleImages() {
    const maxVisible = this.$vuetify.breakpoint.smAndDown ? 3 : 5
    return this.images.slice(0, maxVisible)
  }
}
@media (max-width: 768px) {
  .stack-item {
    transform: scale(0.85) translateY(15px);
  }

  .stack-item.active {
    transform: scale(0.95);
  }
}

3D堆叠效果

添加3D变换增强视觉效果:

.stack-container {
  transform-style: preserve-3d;
}

.stack-item:not(.active) {
  transform: 
    scale(0.9) 
    translateY(20px) 
    rotateX(10deg);
  box-shadow: 0 10px 20px rgba(0,0,0,0.2);
}

.stack-item.active {
  transform: 
    scale(1) 
    translateY(0) 
    rotateX(0);
  box-shadow: 0 15px 30px rgba(0,0,0,0.3);
}

以上方法可以根据实际需求组合使用,调整transform参数和过渡时间可获得不同的视觉效果。对于更复杂的需求,可以考虑使用GSAP等动画库实现更精细的控制。

标签: 图片vue
分享给朋友:

相关文章

vue实现边框

vue实现边框

Vue 实现边框的方法 在 Vue 中实现边框效果可以通过多种方式完成,包括内联样式、CSS 类绑定、动态样式以及使用第三方 UI 库。以下是几种常见的实现方法。 内联样式绑定 使用 Vue 的 :…

vue实现jqueryui

vue实现jqueryui

Vue 实现 jQuery UI 功能 在 Vue 项目中实现类似 jQuery UI 的功能,可以通过原生 Vue 组件或第三方库来实现。以下是几种常见 jQuery UI 功能的 Vue 替代方案…

实现 vue 框架

实现 vue 框架

Vue 框架的基本实现 Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现 Vue 框架的基本步骤和核心概念。 核心概念 响应式数据绑定 Vue 的核心特性之一是响应式…

vue 实现关闭

vue 实现关闭

Vue 实现关闭功能的方法 在Vue中实现关闭功能通常涉及组件销毁、模态框隐藏或路由跳转等场景。以下是几种常见实现方式: 销毁当前组件 使用v-if或v-show控制组件显示/隐藏,适用于局部组件:…

vue实现markdown

vue实现markdown

Vue 实现 Markdown 编辑器 在 Vue 中实现 Markdown 编辑器可以通过集成第三方库或自定义解析器完成。以下是两种常见方法: 使用第三方库(如 marked 或 markdown…

vue实现异步

vue实现异步

Vue 实现异步操作的常见方法 Vue 提供了多种方式来处理异步操作,包括数据获取、事件处理等场景。 使用 async/await 在 Vue 方法中可以直接使用 async/await 语法处理异…