当前位置:首页 > 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 中实现类似社交平台的 @ 功能,通常涉及输入框的监听、用户匹配和选择插入。以下是具体实现方法: 监听输入框内容 使用 v-model 绑定输入框内容,并通过 @…

vue里实现增删改查

vue里实现增删改查

在Vue中实现增删改查 数据初始化 在Vue组件的data中定义初始数据和表单绑定的变量: data() { return { items: [ { id: 1, name:…

vue列表实现

vue列表实现

Vue 列表实现方法 使用 v-for 指令 v-for 是 Vue 中用于渲染列表的核心指令,基于数据源动态生成 DOM 元素。语法格式为 item in items 或 (item, index)…

vue实现socket

vue实现socket

Vue 中实现 WebSocket 通信 在 Vue 项目中实现 WebSocket 通信可以通过原生 WebSocket API 或第三方库(如 socket.io-client)完成。以下是两种常…

vue实现全屏

vue实现全屏

Vue 实现全屏功能的方法 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 来实现。以下是几种常见的实现方式: 使用原生 Fullscreen API 通过调用 doc…

vue 实现分屏

vue 实现分屏

Vue 实现分屏的方法 使用 CSS Flexbox 或 Grid 布局 通过 CSS Flexbox 或 Grid 布局可以轻松实现分屏效果。在 Vue 组件中定义两个或多个区域,并使用 CSS 控…