当前位置:首页 > 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过渡动画

vue实现轮播图动画

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

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

JavaScript动画钩子

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

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

基础实现代码

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实现图片搜索功能 使用HTML5的File API获取图片 通过<input type="file">元素让用户选择图片文件,使用FileReader对象读取图片数据。 <t…

vue 实现复选

vue 实现复选

Vue 实现复选功能 在 Vue 中实现复选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以自动收集用户选择的复选框值。…

vue实现granfana

vue实现granfana

Vue 实现类似 Grafana 的仪表盘 要在 Vue 中实现类似 Grafana 的仪表盘功能,需要结合数据可视化库、状态管理和 UI 组件。以下是关键步骤和推荐工具: 数据可视化库 使用 E…

vue实现粘贴

vue实现粘贴

Vue 实现粘贴功能的方法 在 Vue 中实现粘贴功能通常涉及监听粘贴事件并处理剪贴板数据。以下是几种常见的实现方式: 监听原生粘贴事件 通过 @paste 指令或原生 addEventListen…

甘特图vue实现

甘特图vue实现

甘特图 Vue 实现方法 使用开源库 vue-ganttastic 安装依赖: npm install vue-ganttastic 基础实现代码: <template> <…

vue实现兼容

vue实现兼容

Vue 兼容性实现方案 Vue.js 的兼容性主要涉及对不同浏览器、Vue 版本以及第三方库的支持。以下是常见的兼容性解决方案: 浏览器兼容性 使用 Babel 转换 ES6+ 代码为 ES5 语…