当前位置:首页 > VUE

轮播vue实现

2026-01-08 00:09:54VUE

使用 Vue 实现轮播功能

基础轮播实现

安装依赖(如使用 Swiper):

npm install swiper vue-awesome-swiper

组件代码示例:

<template>
  <swiper :options="swiperOption">
    <swiper-slide v-for="(item, index) in slides" :key="index">
      <img :src="item.image" alt="">
    </swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
  </swiper>
</template>

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

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      slides: [
        { image: require('@/assets/slide1.jpg') },
        { image: require('@/assets/slide2.jpg') }
      ],
      swiperOption: {
        pagination: {
          el: '.swiper-pagination'
        },
        autoplay: {
          delay: 3000
        },
        loop: true
      }
    }
  }
}
</script>

自定义简单轮播

无依赖实现方案:

<template>
  <div class="carousel">
    <div class="slides" :style="slideStyle">
      <div v-for="(slide, index) in slides" :key="index" class="slide">
        <img :src="slide.image" alt="">
      </div>
    </div>
    <button @click="prev">Prev</button>
    <button @click="next">Next</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentIndex: 0,
      slides: [
        { image: require('@/assets/slide1.jpg') },
        { image: require('@/assets/slide2.jpg') }
      ]
    }
  },
  computed: {
    slideStyle() {
      return {
        transform: `translateX(-${this.currentIndex * 100}%)`
      }
    }
  },
  methods: {
    next() {
      this.currentIndex = (this.currentIndex + 1) % this.slides.length
    },
    prev() {
      this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
    }
  }
}
</script>

<style>
.carousel {
  overflow: hidden;
  position: relative;
}
.slides {
  display: flex;
  transition: transform 0.5s ease;
}
.slide {
  min-width: 100%;
}
</style>

自动轮播功能

添加自动播放逻辑:

export default {
  // ...其他代码
  mounted() {
    this.startAutoPlay()
  },
  beforeDestroy() {
    this.stopAutoPlay()
  },
  methods: {
    startAutoPlay() {
      this.interval = setInterval(() => {
        this.next()
      }, 3000)
    },
    stopAutoPlay() {
      clearInterval(this.interval)
    }
  }
}

过渡动画优化

使用 Vue 过渡效果:

<template>
  <transition-group name="slide" tag="div" class="carousel">
    <div v-for="(slide, index) in slides" 
         v-show="index === currentIndex"
         :key="index"
         class="slide">
      <img :src="slide.image" alt="">
    </div>
  </transition-group>
</template>

<style>
.slide-enter-active, .slide-leave-active {
  transition: opacity 0.5s ease;
}
.slide-enter, .slide-leave-to {
  opacity: 0;
}
</style>

响应式处理

添加窗口大小监听:

export default {
  // ...其他代码
  data() {
    return {
      windowWidth: 0
    }
  },
  mounted() {
    this.windowWidth = window.innerWidth
    window.addEventListener('resize', this.handleResize)
  },
  methods: {
    handleResize() {
      this.windowWidth = window.innerWidth
    }
  }
}

轮播vue实现

标签: vue
分享给朋友:

相关文章

vue 实现多选

vue 实现多选

Vue 实现多选的方法 在 Vue 中实现多选功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-model 绑定数组 Vue 的 v-model 指令可以与复选框结合使用,绑定到一个…

vue实现图片轮播

vue实现图片轮播

使用 Swiper 实现图片轮播 安装 Swiper 依赖 npm install swiper 在 Vue 组件中引入 Swiper <template> <div c…

前端vue登录功能实现

前端vue登录功能实现

Vue 登录功能实现步骤 创建登录表单组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含用户名和密码输入框以及提交按钮。 <template> &l…

vue搜索过后实现分页

vue搜索过后实现分页

Vue 实现搜索后分页功能 数据绑定与搜索逻辑 在 Vue 组件中定义必要的数据属性: data() { return { searchQuery: '', currentPa…

vue弹幕实现不重叠

vue弹幕实现不重叠

实现 Vue 弹幕不重叠的方法 CSS 定位与动画控制 通过绝对定位和动态计算弹幕轨道高度,确保弹幕在不同轨道上运行。每条弹幕占据固定高度的轨道,避免重叠。 // 弹幕轨道高度计算 const t…

vue实现简单的弹窗

vue实现简单的弹窗

使用 Vue 实现简单弹窗 组件基础结构 创建一个名为 Modal.vue 的组件文件,包含模板、脚本和样式部分: <template> <div class="modal-…