当前位置:首页 > VUE

vue实现轮训图

2026-02-17 16:19:23VUE

使用Swiper实现轮播图

安装Swiper依赖

npm install swiper vue-awesome-swiper --save

引入Swiper组件

import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/css'
import 'swiper/css/pagination'
import 'swiper/css/navigation'

基本模板结构

<template>
  <swiper
    :slidesPerView="1"
    :spaceBetween="30"
    :loop="true"
    :pagination="{
      clickable: true,
    }"
    :navigation="true"
    :autoplay="{
      delay: 2500,
      disableOnInteraction: false,
    }"
    class="mySwiper"
  >
    <swiper-slide v-for="(item, index) in slides" :key="index">
      <img :src="item.image" />
    </swiper-slide>
  </swiper>
</template>

数据定义

data() {
  return {
    slides: [
      { image: require('@/assets/slide1.jpg') },
      { image: require('@/assets/slide2.jpg') },
      { image: require('@/assets/slide3.jpg') }
    ]
  }
}

纯CSS实现轮播图

HTML结构

<div class="slider">
  <div class="slides">
    <div v-for="(item, index) in slides" 
         :key="index" 
         :class="['slide', { active: currentIndex === index }]">
      <img :src="item.image" />
    </div>
  </div>
  <div class="dots">
    <span v-for="(item, index) in slides" 
          :key="index" 
          :class="{ active: currentIndex === index }"
          @click="currentIndex = index"></span>
  </div>
</div>

CSS样式

.slider {
  position: relative;
  width: 100%;
  height: 400px;
  overflow: hidden;
}

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

.slide {
  min-width: 100%;
  height: 100%;
  position: relative;
  opacity: 0;
  transition: opacity 0.5s ease;
}

.slide.active {
  opacity: 1;
}

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

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

.dots span.active {
  background: white;
}

JavaScript逻辑

data() {
  return {
    currentIndex: 0,
    slides: [
      { image: require('@/assets/slide1.jpg') },
      { image: require('@/assets/slide2.jpg') },
      { image: require('@/assets/slide3.jpg') }
    ],
    interval: null
  }
},
mounted() {
  this.startAutoPlay()
},
methods: {
  startAutoPlay() {
    this.interval = setInterval(() => {
      this.currentIndex = (this.currentIndex + 1) % this.slides.length
    }, 3000)
  },
  stopAutoPlay() {
    clearInterval(this.interval)
  }
},
beforeDestroy() {
  this.stopAutoPlay()
}

响应式轮播图实现

添加响应式断点

const breakpoints = {
  640: {
    slidesPerView: 1
  },
  768: {
    slidesPerView: 2
  },
  1024: {
    slidesPerView: 3
  }
}

在Swiper配置中添加

:breakpoints="breakpoints"

带缩略图的轮播图

添加缩略图Swiper

<swiper
  @swiper="setThumbsSwiper"
  :spaceBetween="10"
  :slidesPerView="4"
  :freeMode="true"
  :watchSlidesProgress="true"
  class="thumbSwiper"
>
  <swiper-slide v-for="(item, index) in slides" :key="index">
    <img :src="item.image" />
  </swiper-slide>
</swiper>

JavaScript关联

methods: {
  setThumbsSwiper(swiper) {
    this.thumbsSwiper = swiper
  }
}

主Swiper添加配置

vue实现轮训图

:thumbs="{ swiper: thumbsSwiper }"

标签: vue
分享给朋友:

相关文章

js实现vue

js实现vue

Vue.js 的基本实现方法 在 JavaScript 中实现 Vue.js 的功能,可以通过直接引入 Vue 库或使用现代构建工具(如 Vite 或 Webpack)。以下是几种常见的实现方式:…

vue实现详情

vue实现详情

Vue 实现详情页的常见方法 使用动态路由 在 Vue Router 中配置动态路由参数,通过 $route.params 获取详情 ID。示例路由配置: { path: '/detail/:i…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json):…

vue实现树目录

vue实现树目录

Vue 实现树形目录 在 Vue 中实现树形目录可以通过递归组件或第三方库(如 element-ui 的 el-tree)完成。以下是两种常见实现方式: 递归组件实现 递归组件适合自定义程度高的树形…

vue实现div单选

vue实现div单选

Vue 实现 div 单选 在 Vue 中实现 div 单选可以通过 v-model 结合 v-for 和事件绑定来完成。以下是具体实现方法: 模板部分 <template> &l…