当前位置:首页 > VUE

vue3实现轮播

2026-01-22 16:49:14VUE

Vue3 实现轮播的方法

使用原生 Vue3 实现轮播

通过 Vue3 的 Composition API 和动态绑定样式实现基础轮播功能。

<template>
  <div class="carousel-container">
    <div class="carousel-track" :style="{ transform: `translateX(${-currentIndex * 100}%)` }">
      <div v-for="(item, index) in items" :key="index" class="carousel-item">
        {{ item }}
      </div>
    </div>
    <button @click="prev">Prev</button>
    <button @click="next">Next</button>
  </div>
</template>

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

const items = ['Slide 1', 'Slide 2', 'Slide 3'];
const currentIndex = ref(0);

const next = () => {
  currentIndex.value = (currentIndex.value + 1) % items.length;
};

const prev = () => {
  currentIndex.value = (currentIndex.value - 1 + items.length) % items.length;
};
</script>

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

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

.carousel-item {
  flex: 0 0 100%;
  min-width: 100%;
}
</style>

使用第三方库 Swiper

Swiper 是一个流行的轮播库,支持 Vue3 集成。

vue3实现轮播

安装 Swiper:

npm install swiper@latest

在 Vue3 中使用:

vue3实现轮播

<template>
  <swiper
    :slides-per-view="1"
    :space-between="50"
    @swiper="onSwiper"
    @slideChange="onSlideChange"
  >
    <swiper-slide>Slide 1</swiper-slide>
    <swiper-slide>Slide 2</swiper-slide>
    <swiper-slide>Slide 3</swiper-slide>
  </swiper>
</template>

<script setup>
import { ref } from 'vue';
import { Swiper, SwiperSlide } from 'swiper/vue';
import 'swiper/css';

const onSwiper = (swiper) => {
  console.log(swiper);
};

const onSlideChange = () => {
  console.log('slide change');
};
</script>

自动轮播功能

通过定时器实现自动轮播,注意组件卸载时清除定时器。

<template>
  <div class="carousel-container">
    <div class="carousel-track" :style="{ transform: `translateX(${-currentIndex * 100}%)` }">
      <div v-for="(item, index) in items" :key="index" class="carousel-item">
        {{ item }}
      </div>
    </div>
  </div>
</template>

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

const items = ['Slide 1', 'Slide 2', 'Slide 3'];
const currentIndex = ref(0);
let intervalId = null;

const startAutoPlay = () => {
  intervalId = setInterval(() => {
    currentIndex.value = (currentIndex.value + 1) % items.length;
  }, 3000);
};

onMounted(() => {
  startAutoPlay();
});

onUnmounted(() => {
  clearInterval(intervalId);
});
</script>

添加指示器和导航按钮

增强用户体验的常见功能实现。

<template>
  <div class="carousel-container">
    <div class="carousel-track" :style="{ transform: `translateX(${-currentIndex * 100}%)` }">
      <div v-for="(item, index) in items" :key="index" class="carousel-item">
        {{ item }}
      </div>
    </div>
    <div class="indicators">
      <span 
        v-for="(item, index) in items" 
        :key="index" 
        :class="{ active: currentIndex === index }"
        @click="goToSlide(index)"
      ></span>
    </div>
    <button class="prev" @click="prev">❮</button>
    <button class="next" @click="next">❯</button>
  </div>
</template>

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

const items = ['Slide 1', 'Slide 2', 'Slide 3'];
const currentIndex = ref(0);

const next = () => {
  currentIndex.value = (currentIndex.value + 1) % items.length;
};

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

const goToSlide = (index) => {
  currentIndex.value = index;
};
</script>

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

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

.indicators span.active {
  background-color: #333;
}

.prev, .next {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(0,0,0,0.5);
  color: white;
  border: none;
  padding: 10px;
  cursor: pointer;
}

.prev {
  left: 10px;
}

.next {
  right: 10px;
}
</style>

分享给朋友:

相关文章

vue实现皮肤插件

vue实现皮肤插件

实现皮肤切换的基本思路 在Vue中实现皮肤插件通常涉及动态切换CSS变量或类名,结合全局状态管理。核心步骤包括定义皮肤配置、管理当前皮肤状态、动态加载样式文件或修改CSS变量。 定义皮肤配置 创建皮…

vue实现按钮刷新

vue实现按钮刷新

Vue 实现按钮刷新功能 在 Vue 中实现按钮刷新功能可以通过多种方式实现,具体取决于刷新需求是局部刷新还是全局刷新。以下是几种常见的方法: 使用 window.location.reload()…

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue聊天实现

vue聊天实现

Vue 聊天应用实现步骤 创建 Vue 项目 使用 Vue CLI 或 Vite 初始化项目,安装必要依赖(如 Vue 3、Element UI 或 Vuetify 用于 UI 组件)。 npm c…

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生改…

vue实现排序

vue实现排序

Vue 实现排序的方法 在 Vue 中实现排序可以通过多种方式完成,以下是一些常见的方法: 使用计算属性排序数组 计算属性非常适合对数据进行排序,因为它会在依赖的数据变化时自动更新。以下是一个示例:…