当前位置:首页 > VUE

基于vue实现轮播效果

2026-01-21 23:27:15VUE

使用 vue-awesome-swiper 实现轮播

安装 vue-awesome-swiper 依赖包:

npm install swiper vue-awesome-swiper --save

在组件中引入并使用:

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

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      swiperOptions: {
        loop: true,
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        },
        pagination: {
          el: '.swiper-pagination',
          clickable: true
        },
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        }
      },
      slides: [
        { id: 1, img: 'image1.jpg' },
        { id: 2, img: 'image2.jpg' },
        { id: 3, img: 'image3.jpg' }
      ]
    }
  }
}

模板部分:

<swiper :options="swiperOptions">
  <swiper-slide v-for="slide in slides" :key="slide.id">
    <img :src="slide.img" alt="">
  </swiper-slide>
  <div class="swiper-pagination" slot="pagination"></div>
  <div class="swiper-button-prev" slot="button-prev"></div>
  <div class="swiper-button-next" slot="button-next"></div>
</swiper>

使用纯 Vue 实现基础轮播

创建基础轮播组件:

export default {
  data() {
    return {
      currentIndex: 0,
      slides: [
        { id: 1, img: 'image1.jpg' },
        { id: 2, img: 'image2.jpg' },
        { id: 3, img: 'image3.jpg' }
      ],
      interval: null
    }
  },
  mounted() {
    this.startAutoPlay()
  },
  beforeDestroy() {
    clearInterval(this.interval)
  },
  methods: {
    startAutoPlay() {
      this.interval = setInterval(() => {
        this.nextSlide()
      }, 3000)
    },
    nextSlide() {
      this.currentIndex = (this.currentIndex + 1) % this.slides.length
    },
    prevSlide() {
      this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
    },
    goToSlide(index) {
      this.currentIndex = index
    }
  }
}

模板部分:

<div class="carousel">
  <div class="slides-container">
    <div 
      v-for="(slide, index) in slides" 
      :key="slide.id" 
      class="slide" 
      :class="{ active: index === currentIndex }"
    >
      <img :src="slide.img" alt="">
    </div>
  </div>
  <button @click="prevSlide">上一张</button>
  <button @click="nextSlide">下一张</button>
  <div class="dots">
    <span 
      v-for="(slide, index) in slides" 
      :key="slide.id" 
      @click="goToSlide(index)"
      :class="{ active: index === currentIndex }"
    ></span>
  </div>
</div>

样式部分:

.carousel {
  position: relative;
  width: 100%;
  height: 300px;
  overflow: hidden;
}

.slides-container {
  display: flex;
  width: 100%;
  height: 100%;
}

.slide {
  width: 100%;
  height: 100%;
  flex-shrink: 0;
  opacity: 0;
  transition: opacity 0.5s ease;
}

.slide.active {
  opacity: 1;
}

.dots span {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin: 0 5px;
  border-radius: 50%;
  background: #ccc;
  cursor: pointer;
}

.dots span.active {
  background: #333;
}

使用 CSS 过渡效果增强

在纯 Vue 实现基础上,修改样式部分实现平滑过渡:

.slides-container {
  position: relative;
  height: 100%;
}

.slide {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  transition: opacity 0.5s ease, transform 0.5s ease;
}

.slide.active {
  opacity: 1;
  transform: translateX(0);
}

.slide.next {
  transform: translateX(100%);
}

.slide.prev {
  transform: translateX(-100%);
}

修改 JavaScript 部分实现滑动效果:

methods: {
  nextSlide() {
    const slides = document.querySelectorAll('.slide')
    slides[this.currentIndex].classList.remove('active')
    slides[this.currentIndex].classList.add('prev')

    this.currentIndex = (this.currentIndex + 1) % this.slides.length

    slides[this.currentIndex].classList.remove('next')
    slides[this.currentIndex].classList.add('active')
  },
  prevSlide() {
    const slides = document.querySelectorAll('.slide')
    slides[this.currentIndex].classList.remove('active')
    slides[this.currentIndex].classList.add('next')

    this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length

    slides[this.currentIndex].classList.remove('prev')
    slides[this.currentIndex].classList.add('active')
  }
}

基于vue实现轮播效果

标签: 效果vue
分享给朋友:

相关文章

简易实现vue

简易实现vue

简易 Vue 实现思路 核心是数据劫持结合发布-订阅模式,通过 Object.defineProperty 或 Proxy 监听数据变化,触发视图更新。 数据劫持实现 class Observer…

vue实现检测数组

vue实现检测数组

Vue 中检测数组变化的方法 Vue 的响应式系统无法直接检测到数组的某些变化,因为 JavaScript 的限制。以下是一些解决方案: 使用 Vue.set 或 this.$set Vue 提供…

vue伪函数实现

vue伪函数实现

Vue 伪函数实现方法 伪函数(Mock Function)在 Vue 中常用于测试或模拟某些逻辑,以下是几种常见的实现方式: 使用 Jest 的 jest.fn() 在单元测试中,可以通过 Jes…

vue实现收起展开面板

vue实现收起展开面板

实现收起展开面板的方法 在Vue中实现收起展开面板功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-show或v-if控制显示隐藏 利用Vue的指令可以轻松实现面板的展开和收起:…

vue实现画圆弧并着色

vue实现画圆弧并着色

在 Vue 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…

vue调用接口实现退出

vue调用接口实现退出

实现Vue退出功能的步骤 在Vue中调用接口实现退出功能通常需要与后端API交互,以下是实现方法: 前端实现 创建退出方法,调用后端API接口: methods: { logout() {…