当前位置:首页 > VUE

vue实现轮播图代码

2026-01-21 12:47:11VUE

Vue实现轮播图代码

以下是一个基于Vue 3的轮播图实现示例,包含自动轮播、手动切换和指示器功能。

基础结构

<template>
  <div class="carousel-container">
    <div class="carousel" @mouseenter="pause" @mouseleave="resume">
      <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.alt">
        </div>
      </div>
      <button class="prev" @click="prev">‹</button>
      <button class="next" @click="next">›</button>
      <div class="indicators">
        <span 
          v-for="(item, index) in slides" 
          :key="index" 
          :class="{ active: currentIndex === index }"
          @click="goTo(index)"
        ></span>
      </div>
    </div>
  </div>
</template>

脚本部分

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

export default {
  setup() {
    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);
    let intervalId = null;
    const autoPlayDelay = 3000;

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

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

    const goTo = (index) => {
      currentIndex.value = index;
    };

    const startAutoPlay = () => {
      intervalId = setInterval(next, autoPlayDelay);
    };

    const pause = () => {
      clearInterval(intervalId);
    };

    const resume = () => {
      startAutoPlay();
    };

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

    onUnmounted(() => {
      pause();
    });

    return {
      slides,
      currentIndex,
      next,
      prev,
      goTo,
      pause,
      resume
    };
  }
};
</script>

样式部分

<style scoped>
.carousel-container {
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  overflow: hidden;
  position: relative;
}

.carousel {
  position: relative;
  height: 400px;
}

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

.slide {
  min-width: 100%;
  height: 100%;
}

.slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

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

.prev {
  left: 10px;
}

.next {
  right: 10px;
}

.indicators {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
}

.indicators span {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: rgba(255,255,255,0.5);
  cursor: pointer;
}

.indicators span.active {
  background: white;
}
</style>

功能说明

  1. 自动轮播:组件挂载后自动开始轮播,3秒切换一次
  2. 鼠标悬停暂停:鼠标移入轮播区域时暂停自动播放
  3. 导航按钮:提供上一张/下一张的手动切换按钮
  4. 指示器:底部圆点指示当前幻灯片位置,可点击跳转
  5. 平滑过渡:使用CSS transition实现平滑的滑动效果

扩展建议

  1. 添加淡入淡出效果:修改CSS transition属性
  2. 无限循环:克隆首尾幻灯片实现无缝循环
  3. 响应式设计:添加媒体查询适应不同屏幕尺寸
  4. 懒加载:对未显示的图片实现懒加载
  5. 触摸支持:添加touch事件处理移动端滑动

vue实现轮播图代码

标签: 代码vue
分享给朋友:

相关文章

vue实现前端分离

vue实现前端分离

Vue 实现前后端分离 前后端分离是一种架构模式,前端负责页面渲染和交互逻辑,后端提供数据接口。Vue.js 作为前端框架,非常适合实现前后端分离的开发模式。 核心实现步骤 创建 Vue 项目 使…

vue实现视窗

vue实现视窗

Vue 实现视窗功能 在 Vue 中实现视窗功能通常涉及监听浏览器窗口大小变化、响应式布局或创建自定义弹窗组件。以下是几种常见实现方式: 监听浏览器窗口大小变化 使用 Vue 的 mounted…

eventbus vue实现

eventbus vue实现

EventBus 实现原理 EventBus 是一种发布/订阅模式,用于 Vue 组件间的通信。核心是通过一个中央事件总线管理事件监听和触发。 创建 EventBus 在 Vue 项目中创建一个独…

vue实现ai

vue实现ai

Vue 实现 AI 功能 Vue.js 可以通过集成第三方 AI 服务或本地 AI 模型来实现 AI 功能。以下是几种常见的方法: 集成第三方 AI API 使用 Vue 调用如 OpenAI、G…

jq 实现 vue

jq 实现 vue

jq 实现 Vue 的核心功能 jq 是一个轻量级的 JavaScript 库,主要用于 DOM 操作和事件处理。虽然它不能完全替代 Vue,但可以通过一些技巧模拟 Vue 的核心功能,如数据绑定、事…

vue实现报表

vue实现报表

vue实现报表的方法 使用Vue实现报表通常需要结合图表库或表格组件。以下是几种常见方法: 使用ECharts 安装ECharts库: npm install echarts vue-echa…