当前位置:首页 > 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实现tab换行

vue实现tab换行

Vue实现Tab切换的常见方法 使用Vue实现Tab切换功能可以通过多种方式完成,以下是几种常见的方法: 动态组件结合v-if或v-show 通过v-if或v-show控制不同Tab内容的显示与隐…

vue 页签实现

vue 页签实现

Vue 页签实现方法 在 Vue 中实现页签功能通常涉及动态组件、路由或状态管理。以下是几种常见的实现方式: 使用动态组件 通过 Vue 的 <component> 动态组件结合 v-…

vue如何实现到期提醒

vue如何实现到期提醒

实现 Vue 到期提醒功能 使用计算属性计算剩余时间 在 Vue 组件中定义一个计算属性,计算目标日期与当前日期的差值。通过 Date 对象获取时间戳并计算剩余天数或小时数。 computed: {…

vue网页实现拖动效果

vue网页实现拖动效果

Vue 实现拖动效果的方法 使用 HTML5 拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性和相关事件实现拖动效果。 <template>…

vue实现多行多个表单

vue实现多行多个表单

实现多行多个表单的方法 在Vue中实现多行多个表单通常涉及动态表单生成、数据绑定和表单验证。以下是几种常见的方法: 动态表单数组绑定 使用v-for指令动态生成多行表单,并通过数组管理表单数据。在…

vue实现弹窗可切换

vue实现弹窗可切换

实现弹窗可切换的 Vue 方案 动态组件切换 通过 Vue 的 <component :is="currentComponent"> 动态加载不同弹窗组件,结合 v-if 控制显示状态。…