当前位置:首页 > VUE

vue实现div轮播

2026-01-18 10:06:13VUE

Vue实现div轮播的方法

使用vue-awesome-swiper插件

安装vue-awesome-swiper插件:

npm install swiper vue-awesome-swiper --save

在组件中引入并使用:

import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      swiperOptions: {
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        },
        loop: true,
        pagination: {
          el: '.swiper-pagination',
          clickable: true
        }
      },
      slides: [
        { id: 1, content: 'Slide 1' },
        { id: 2, content: 'Slide 2' },
        { id: 3, content: 'Slide 3' }
      ]
    }
  }
}

模板部分:

<swiper :options="swiperOptions">
  <swiper-slide v-for="slide in slides" :key="slide.id">
    <div class="slide-content">{{ slide.content }}</div>
  </swiper-slide>
  <div class="swiper-pagination" slot="pagination"></div>
</swiper>

纯Vue实现轮播

创建基础轮播组件:

export default {
  data() {
    return {
      currentIndex: 0,
      slides: [
        { id: 1, content: 'Slide 1' },
        { id: 2, content: 'Slide 2' },
        { id: 3, content: 'Slide 3' }
      ],
      timer: null
    }
  },
  mounted() {
    this.startAutoPlay()
  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  methods: {
    startAutoPlay() {
      this.timer = setInterval(() => {
        this.nextSlide()
      }, 3000)
    },
    nextSlide() {
      this.currentIndex = (this.currentIndex + 1) % this.slides.length
    },
    prevSlide() {
      this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
    },
    goToSlide(index) {
      this.currentIndex = index
    }
  }
}

模板部分:

<div class="carousel-container">
  <div class="carousel-slides" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
    <div class="slide" v-for="(slide, index) in slides" :key="slide.id">
      {{ slide.content }}
    </div>
  </div>
  <button class="prev-btn" @click="prevSlide">Prev</button>
  <button class="next-btn" @click="nextSlide">Next</button>
  <div class="indicators">
    <span 
      v-for="(slide, index) in slides" 
      :key="index" 
      @click="goToSlide(index)"
      :class="{ active: currentIndex === index }"
    ></span>
  </div>
</div>

CSS样式:

.carousel-container {
  position: relative;
  overflow: hidden;
  width: 100%;
  height: 300px;
}

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

.slide {
  flex: 0 0 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
}

.prev-btn, .next-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 10;
}

.prev-btn {
  left: 10px;
}

.next-btn {
  right: 10px;
}

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

.indicators span {
  width: 12px;
  height: 12px;
  margin: 0 5px;
  border-radius: 50%;
  background: #ccc;
  cursor: pointer;
}

.indicators span.active {
  background: #333;
}

使用Vue Transition组件实现动画效果

在纯Vue实现的基础上添加过渡效果:

<div class="carousel-container">
  <transition-group 
    name="slide" 
    tag="div" 
    class="carousel-slides"
  >
    <div 
      class="slide" 
      v-for="(slide, index) in slides" 
      :key="slide.id"
      v-show="currentIndex === index"
    >
      {{ slide.content }}
    </div>
  </transition-group>
</div>

添加过渡CSS:

.slide-enter-active, .slide-leave-active {
  transition: opacity 0.5s ease;
  position: absolute;
  width: 100%;
}

.slide-enter, .slide-leave-to {
  opacity: 0;
}

vue实现div轮播

标签: vuediv
分享给朋友:

相关文章

vue实现屏幕共享

vue实现屏幕共享

Vue 实现屏幕共享的方法 使用 WebRTC 和 getDisplayMedia API 通过 navigator.mediaDevices.getDisplayMedia 捕获屏幕内容,结合 We…

vue怎么实现选中删除

vue怎么实现选中删除

实现选中删除功能的方法 在Vue中实现选中删除功能通常涉及以下几个关键步骤: 数据绑定与选中状态管理 使用v-model或v-for指令绑定列表数据,结合复选框或单选按钮实现选中状态管理。例如:…

vue实现矩阵

vue实现矩阵

Vue 实现矩阵的方法 在 Vue 中实现矩阵操作可以通过多种方式完成,以下是一些常见的方法: 使用二维数组表示矩阵 矩阵可以用二维数组来表示,每个元素对应矩阵中的一个值。在 Vue 的 data…

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sele…

vue实现slider

vue实现slider

Vue 实现 Slider 组件 使用 Vue 实现 Slider 组件可以通过原生开发或借助第三方库。以下是两种常见方法: 原生实现 Slider 创建一个基础的 Slider 组件,通过 v-…

vue vnode实现

vue vnode实现

Vue VNode 实现原理 VNode(Virtual Node)是 Vue 的核心概念之一,用于描述真实 DOM 的轻量级虚拟表示。VNode 的实现涉及以下关键点: VNode 的基本结构…