当前位置:首页 > VUE

vue实现轮播图代码

2026-01-21 12:47:11VUE

Vue实现轮播图代码

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

vue实现轮播图代码

基础结构

<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 Router Vue Router 是 Vue.js 官方提供的…

vue实现点击

vue实现点击

实现点击事件的基本方法 在Vue中实现点击事件可以通过v-on指令或@简写方式绑定。以下是常见的点击事件绑定方法: <template> <button v-on:click=…

vue实现选区

vue实现选区

Vue 实现选区的基本方法 在Vue中实现选区功能通常涉及DOM操作和事件处理。以下是几种常见的方法: 使用原生JavaScript的Selection API 通过window.getSelec…

vue插槽实现

vue插槽实现

插槽的基本概念 Vue插槽(Slot)是一种内容分发机制,允许父组件向子组件传递模板片段,子组件通过<slot>标签定义接收位置。插槽的核心作用是增强组件的灵活性和复用性。 默认插槽 子…

vue实现盖章

vue实现盖章

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

mui实现vue

mui实现vue

mui 实现 Vue 的步骤 mui 是一个轻量级的前端框架,常用于移动端开发。结合 Vue 可以实现高效的开发体验。以下是具体实现方法。 安装 mui 和 Vue 通过 npm 或 yarn 安装…