当前位置:首页 > VUE

vue实现简单轮播图

2026-02-25 07:53:47VUE

Vue 实现简单轮播图

使用 Vue 3 的 Composition API 实现基础轮播图

<template>
  <div class="carousel-container">
    <div class="carousel" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
      <div class="slide" v-for="(slide, index) in slides" :key="index">
        <img :src="slide.image" :alt="slide.alt">
      </div>
    </div>
    <button @click="prevSlide">Previous</button>
    <button @click="nextSlide">Next</button>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';

const slides = ref([
  { image: 'image1.jpg', alt: 'Image 1' },
  { image: 'image2.jpg', alt: 'Image 2' },
  { image: 'image3.jpg', alt: 'Image 3' }
]);

const currentIndex = ref(0);

const nextSlide = () => {
  currentIndex.value = (currentIndex.value + 1) % slides.value.length;
};

const prevSlide = () => {
  currentIndex.value = (currentIndex.value - 1 + slides.value.length) % slides.value.length;
};

onMounted(() => {
  setInterval(nextSlide, 3000);
});
</script>

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

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

.slide {
  min-width: 100%;
}

.slide img {
  width: 100%;
  height: auto;
}
</style>

使用 Vue 2 的 Options API 实现

<template>
  <div class="carousel">
    <transition-group name="slide" tag="div">
      <div v-for="(item, index) in slides" :key="index" v-show="currentSlide === index">
        <img :src="item.image" :alt="item.alt">
      </div>
    </transition-group>
    <button @click="prev">Prev</button>
    <button @click="next">Next</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentSlide: 0,
      slides: [
        { image: 'image1.jpg', alt: 'Image 1' },
        { image: 'image2.jpg', alt: 'Image 2' },
        { image: 'image3.jpg', alt: 'Image 3' }
      ]
    };
  },
  methods: {
    next() {
      this.currentSlide = (this.currentSlide + 1) % this.slides.length;
    },
    prev() {
      this.currentSlide = (this.currentSlide - 1 + this.slides.length) % this.slides.length;
    }
  },
  mounted() {
    setInterval(this.next, 3000);
  }
};
</script>

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

.slide-enter-active, .slide-leave-active {
  transition: all 1s ease;
}
.slide-enter {
  transform: translateX(100%);
}
.slide-leave-to {
  transform: translateX(-100%);
}

img {
  width: 100%;
}
</style>

添加指示器(小圆点导航)

在 Vue 3 版本中添加以下代码:

vue实现简单轮播图

<div class="indicators">
  <span 
    v-for="(_, index) in slides" 
    :key="index" 
    @click="currentIndex = index"
    :class="{ active: currentIndex === index }"
  ></span>
</div>

<style>
.indicators {
  display: flex;
  justify-content: center;
  margin-top: 10px;
}

.indicators span {
  width: 12px;
  height: 12px;
  margin: 0 5px;
  border-radius: 50%;
  background-color: #ccc;
  cursor: pointer;
}

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

实现无限循环效果

修改 Vue 3 的 nextSlide 和 prevSlide 方法:

vue实现简单轮播图

const nextSlide = () => {
  if (currentIndex.value >= slides.value.length - 1) {
    currentIndex.value = 0;
  } else {
    currentIndex.value++;
  }
};

const prevSlide = () => {
  if (currentIndex.value <= 0) {
    currentIndex.value = slides.value.length - 1;
  } else {
    currentIndex.value--;
  }
};

使用第三方库 vue-awesome-swiper

安装:

npm install swiper vue-awesome-swiper

示例代码:

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

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

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

标签: 简单vue
分享给朋友:

相关文章

vue实现静态查询

vue实现静态查询

Vue 实现静态查询 在 Vue 中实现静态查询通常指不依赖后端接口,直接在前端处理数据筛选或搜索功能。以下是几种常见实现方式: 使用计算属性过滤数据 通过计算属性对静态数据进行筛选,适合简单查询场…

vue实现a4打印模板

vue实现a4打印模板

使用Vue实现A4打印模板 在Vue中实现A4打印模板需要结合CSS的打印样式和Vue的模板渲染能力。以下是具体实现方法: 设置A4纸张尺寸 在CSS中定义A4纸张的标准尺寸(210mm × 297…

vue datepicker 实现

vue datepicker 实现

实现 Vue Datepicker 的基本方法 在 Vue 项目中实现日期选择功能,可以使用第三方库如 vue-datepicker 或 v-calendar。以下是两种常见实现方式: 安装 vue…

vue tag实现

vue tag实现

Vue 标签实现方法 在 Vue 中实现标签功能可以通过多种方式完成,常见场景包括动态标签生成、标签输入框、标签管理等。以下是几种典型实现方案: 动态标签列表渲染 使用 v-for 指令渲染标签数组…

vue 实现轮播

vue 实现轮播

Vue 实现轮播的方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template&…

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…