当前位置:首页 > VUE

vue实现幻灯

2026-01-13 06:27:10VUE

Vue实现幻灯片

使用第三方库(推荐)

安装 vue-awesome-swiper 库:

npm install swiper vue-awesome-swiper

基本实现代码:

<template>
  <swiper :options="swiperOption">
    <swiper-slide v-for="(slide, index) in slides" :key="index">
      <img :src="slide.image" :alt="slide.title">
    </swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
    <div class="swiper-button-prev" slot="button-prev"></div>
    <div class="swiper-button-next" slot="button-next"></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: '/image1.jpg', title: 'Slide 1' },
        { image: '/image2.jpg', title: 'Slide 2' },
        { image: '/image3.jpg', title: 'Slide 3' }
      ],
      swiperOption: {
        pagination: {
          el: '.swiper-pagination',
          clickable: true
        },
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        },
        loop: true,
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        }
      }
    }
  }
}
</script>

自定义实现

基本结构:

<template>
  <div class="slider-container">
    <div class="slider" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
      <div class="slide" v-for="(slide, index) in slides" :key="index">
        <img :src="slide.image" :alt="slide.title">
      </div>
    </div>
    <button @click="prevSlide">Prev</button>
    <button @click="nextSlide">Next</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentIndex: 0,
      slides: [
        { image: '/image1.jpg', title: 'Slide 1' },
        { image: '/image2.jpg', title: 'Slide 2' },
        { image: '/image3.jpg', title: 'Slide 3' }
      ]
    }
  },
  methods: {
    nextSlide() {
      this.currentIndex = (this.currentIndex + 1) % this.slides.length
    },
    prevSlide() {
      this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
    }
  },
  mounted() {
    setInterval(this.nextSlide, 3000)
  }
}
</script>

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

添加过渡效果

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

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

响应式设计

<script>
export default {
  data() {
    return {
      windowWidth: 0
    }
  },
  mounted() {
    this.windowWidth = window.innerWidth
    window.addEventListener('resize', this.handleResize)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize)
  },
  methods: {
    handleResize() {
      this.windowWidth = window.innerWidth
    }
  },
  computed: {
    slidesToShow() {
      return this.windowWidth < 768 ? 1 : 3
    }
  }
}
</script>

vue实现幻灯

标签: 幻灯vue
分享给朋友:

相关文章

vue实现图片轮播

vue实现图片轮播

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

vue实现图片分析

vue实现图片分析

Vue 实现图片分析的方法 在 Vue 中实现图片分析通常涉及图片上传、预览、后端交互以及分析结果展示等功能。以下是几种常见的实现方式: 使用 HTML5 File API 和 Canvas…

vue实现放大效果

vue实现放大效果

使用 CSS transform 实现放大效果 在 Vue 中实现放大效果可以通过 CSS 的 transform: scale() 属性。这种方法简单高效,适合大多数交互场景。 定义一个 CSS…

vue实现选择季度

vue实现选择季度

Vue 实现选择季度的几种方法 使用下拉选择框(Select) 在 Vue 中可以通过 v-model 绑定一个下拉选择框来实现季度选择。数据可以预先定义为一个包含季度选项的数组。 <…

vue实现压缩上传文件

vue实现压缩上传文件

压缩上传文件的实现方法 在Vue中实现文件压缩和上传功能,可以通过以下步骤完成。该方法结合了前端文件压缩库和HTTP请求,确保文件在上传前被有效压缩。 安装必要的依赖 需要使用compressor…

vue中如何实现循环

vue中如何实现循环

在 Vue 中实现循环 Vue 提供了 v-for 指令用于实现循环渲染列表数据。v-for 可以遍历数组、对象或数字范围,生成动态的 DOM 元素。 遍历数组 使用 v-for 遍历数组时,语法为…