当前位置:首页 > VUE

vue前端实现轮播图

2026-01-22 05:43:50VUE

使用 Vue 实现轮播图

基于原生 Vue 实现

模板部分

<template>
  <div class="carousel">
    <div class="slides" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
      <div v-for="(slide, index) in slides" :key="index" class="slide">
        <img :src="slide.image" :alt="slide.title">
      </div>
    </div>
    <button class="prev" @click="prevSlide">❮</button>
    <button class="next" @click="nextSlide">❯</button>
    <div class="indicators">
      <span 
        v-for="(slide, index) in slides" 
        :key="index" 
        @click="goToSlide(index)"
        :class="{ active: currentIndex === index }"
      ></span>
    </div>
  </div>
</template>

脚本部分

<script>
export default {
  data() {
    return {
      currentIndex: 0,
      slides: [
        { image: 'image1.jpg', title: 'Slide 1' },
        { image: 'image2.jpg', title: 'Slide 2' },
        { image: 'image3.jpg', title: 'Slide 3' }
      ],
      autoPlayInterval: null
    }
  },
  methods: {
    prevSlide() {
      this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
    },
    nextSlide() {
      this.currentIndex = (this.currentIndex + 1) % this.slides.length
    },
    goToSlide(index) {
      this.currentIndex = index
    },
    startAutoPlay() {
      this.autoPlayInterval = setInterval(this.nextSlide, 3000)
    },
    stopAutoPlay() {
      clearInterval(this.autoPlayInterval)
    }
  },
  mounted() {
    this.startAutoPlay()
  },
  beforeUnmount() {
    this.stopAutoPlay()
  }
}
</script>

样式部分

<style scoped>
.carousel {
  position: relative;
  width: 100%;
  overflow: hidden;
}

.slides {
  display: flex;
  transition: transform 0.5s ease;
}

.slide {
  min-width: 100%;
  box-sizing: border-box;
}

.slide img {
  width: 100%;
  height: auto;
}

.prev, .next {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  padding: 16px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
  background-color: rgba(0,0,0,0.5);
  border: none;
}

.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

.indicators {
  text-align: center;
  position: absolute;
  bottom: 10px;
  width: 100%;
}

.indicators span {
  display: inline-block;
  width: 12px;
  height: 12px;
  margin: 0 4px;
  background-color: #bbb;
  border-radius: 50%;
  cursor: pointer;
}

.indicators span.active {
  background-color: #717171;
}
</style>

使用 Vue 轮播图库

安装 Vue-Awesome-Swiper

npm install swiper vue-awesome-swiper --save

组件实现

vue前端实现轮播图

<template>
  <swiper :options="swiperOption" ref="mySwiper">
    <swiper-slide v-for="(slide, index) in slides" :key="index">
      <img :src="slide.image" :alt="slide.title">
    </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>
</template>

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

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      slides: [
        { image: 'image1.jpg', title: 'Slide 1' },
        { image: 'image2.jpg', title: 'Slide 2' },
        { image: 'image3.jpg', title: 'Slide 3' }
      ],
      swiperOption: {
        pagination: {
          el: '.swiper-pagination',
          clickable: true
        },
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        },
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        },
        loop: true
      }
    }
  }
}
</script>

关键实现要点

动画效果处理 CSS transition 属性实现平滑滑动效果

.slides {
  transition: transform 0.5s ease;
}

无限循环实现 通过取模运算实现无限循环

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

响应式设计 确保轮播图在不同屏幕尺寸下正常显示

vue前端实现轮播图

.slide img {
  width: 100%;
  height: auto;
  max-height: 500px;
  object-fit: cover;
}

触摸事件支持 使用第三方库如Swiper可自动支持触摸滑动

性能优化 图片懒加载实现

swiperOption: {
  lazy: {
    loadPrevNext: true
  }
}

自动播放控制 组件挂载时启动定时器,销毁时清除

mounted() {
  this.startAutoPlay()
},
beforeUnmount() {
  this.stopAutoPlay()
}

标签: vue轮播图
分享给朋友:

相关文章

实现vue

实现vue

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script src…

vue实现盖章

vue实现盖章

Vue 实现盖章功能 在Vue中实现盖章功能通常涉及前端UI交互,通过Canvas或SVG绘制印章,并允许用户拖拽或点击放置印章。以下是具体实现方法: 使用Canvas绘制印章 通过HTML5的Ca…

vue实现节流

vue实现节流

vue实现节流的方法 在Vue中实现节流(throttle)功能,通常用于限制高频事件的触发频率,例如滚动、输入或按钮点击。以下是几种常见的实现方式: 使用Lodash的throttle函数 安装L…

vue实现长按

vue实现长按

Vue 实现长按功能的方法 在 Vue 中实现长按功能可以通过原生事件监听或自定义指令完成。以下是几种常见实现方式: 使用原生事件监听 通过 @mousedown 和 @mouseup 或 @tou…

vue实现datalist

vue实现datalist

使用 Vue 实现 HTML5 的 datalist 功能 HTML5 的 <datalist> 元素提供了一种自动完成输入框的功能,允许用户在输入时从预定义的选项列表中选择。以下是几种在…

vue 实现登录

vue 实现登录

Vue 实现登录功能 创建登录表单组件 在 Vue 项目中创建一个登录表单组件(如 Login.vue),包含用户名和密码输入框以及提交按钮。使用 v-model 实现双向数据绑定。 <tem…