当前位置:首页 > VUE

vue实现轮播图动画

2026-02-21 00:45:54VUE

使用 Vue 实现轮播图动画

基础轮播图实现

安装依赖(如需要)

npm install vue-awesome-swiper

基础模板结构

<template>
  <swiper :options="swiperOption">
    <swiper-slide v-for="(slide, index) in slides" :key="index">
      <img :src="slide.image" alt="">
    </swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      slides: [
        { image: '/path/to/image1.jpg' },
        { image: '/path/to/image2.jpg' }
      ],
      swiperOption: {
        pagination: {
          el: '.swiper-pagination'
        },
        loop: true,
        autoplay: {
          delay: 3000
        }
      }
    }
  }
}
</script>

自定义动画效果

CSS过渡动画

.swiper-slide {
  transition: transform 0.5s ease-in-out;
}

.swiper-slide-active {
  transform: scale(1.05);
}

JavaScript动画钩子

swiperOption: {
  on: {
    slideChangeTransitionStart: function() {
      // 动画开始时的处理
    },
    slideChangeTransitionEnd: function() {
      // 动画结束时的处理
    }
  }
}

纯Vue实现(不使用第三方库)

基础实现代码

<template>
  <div class="carousel">
    <div class="slides" :style="slideStyle">
      <div v-for="(slide, index) in slides" 
           :key="index" 
           class="slide">
        <img :src="slide.image">
      </div>
    </div>
    <button @click="prev">Prev</button>
    <button @click="next">Next</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentIndex: 0,
      slides: [
        { image: '/path/to/image1.jpg' },
        { image: '/path/to/image2.jpg' }
      ]
    }
  },
  computed: {
    slideStyle() {
      return {
        transform: `translateX(-${this.currentIndex * 100}%)`,
        transition: 'transform 0.5s ease-in-out'
      }
    }
  },
  methods: {
    next() {
      this.currentIndex = (this.currentIndex + 1) % this.slides.length
    },
    prev() {
      this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
    }
  }
}
</script>

<style>
.carousel {
  overflow: hidden;
  position: relative;
}
.slides {
  display: flex;
  width: 100%;
}
.slide {
  flex-shrink: 0;
  width: 100%;
}
</style>

高级动画效果

淡入淡出效果

.slide {
  position: absolute;
  opacity: 0;
  transition: opacity 1s ease-in-out;
}

.slide.active {
  opacity: 1;
}

3D翻转效果

.slide {
  transform-style: preserve-3d;
  transition: transform 1s;
}

.slide.active {
  transform: rotateY(0deg);
}

.slide.next {
  transform: rotateY(180deg);
}

自动轮播实现

添加自动轮播功能

export default {
  mounted() {
    this.startAutoPlay()
  },
  beforeDestroy() {
    this.stopAutoPlay()
  },
  methods: {
    startAutoPlay() {
      this.interval = setInterval(() => {
        this.next()
      }, 3000)
    },
    stopAutoPlay() {
      clearInterval(this.interval)
    }
  }
}

vue实现轮播图动画

标签: 动画vue
分享给朋友:

相关文章

vue实现分类

vue实现分类

Vue实现分类功能的方法 使用v-for和计算属性 通过计算属性对数据进行分类处理,结合v-for渲染分类后的列表。示例代码: <template> <div>…

vue实现机制

vue实现机制

Vue 实现机制的核心原理 Vue.js 的核心实现机制主要基于响应式系统、虚拟 DOM 和组件化设计。以下是关键实现细节: 响应式系统 Vue 通过 Object.defineProperty(V…

vue 动画实现

vue 动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要分为内置组件和第三方库集成。 使用 Vue 内置过渡组件 Vue 的 <transition> 和 <transiti…

vue实现Siri

vue实现Siri

Vue 实现 Siri 风格语音交互 实现类似 Siri 的语音交互功能需要结合语音识别、语音合成和前端交互逻辑。Vue 作为前端框架,可以很好地管理这些功能的交互状态。 语音识别集成 使用 Web…

vue实现pwa

vue实现pwa

Vue 实现 PWA 的步骤 Vue 结合 PWA(Progressive Web App)可以创建离线可用的 Web 应用。以下是实现方法: 安装 Vue CLI 并创建项目 确保已安装 Vue…

vue实现追加

vue实现追加

追加数据到数组或列表 在Vue中追加数据到数组或列表,可以通过push方法或concat方法实现。以下是几种常见的实现方式: 方法一:使用push方法 this.items.push(newI…