当前位置:首页 > VUE

vue实现滑动图片

2026-03-09 07:42:02VUE

Vue 实现滑动图片的方法

使用第三方库(如 Swiper)

安装 Swiper 库:

vue实现滑动图片

npm install swiper

在 Vue 组件中引入 Swiper:

vue实现滑动图片

<template>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="(image, index) in images" :key="index">
        <img :src="image.src" :alt="image.alt">
      </div>
    </div>
    <div class="swiper-pagination"></div>
    <div class="swiper-button-next"></div>
    <div class="swiper-button-prev"></div>
  </div>
</template>

<script>
import Swiper from 'swiper';
import 'swiper/swiper-bundle.min.css';

export default {
  data() {
    return {
      images: [
        { src: 'image1.jpg', alt: 'Image 1' },
        { src: 'image2.jpg', alt: 'Image 2' },
        { src: 'image3.jpg', alt: 'Image 3' }
      ]
    };
  },
  mounted() {
    new Swiper('.swiper-container', {
      loop: true,
      pagination: {
        el: '.swiper-pagination',
        clickable: true,
      },
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
    });
  }
};
</script>

<style>
.swiper-container {
  width: 100%;
  height: 300px;
}
.swiper-slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>

使用纯 CSS 实现滑动效果

通过 CSS 的 overflowscroll-snap 属性实现简单滑动:

<template>
  <div class="slider">
    <div class="slides" ref="slides">
      <div class="slide" v-for="(image, index) in images" :key="index">
        <img :src="image.src" :alt="image.alt">
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: [
        { src: 'image1.jpg', alt: 'Image 1' },
        { src: 'image2.jpg', alt: 'Image 2' },
        { src: 'image3.jpg', alt: 'Image 3' }
      ]
    };
  }
};
</script>

<style>
.slider {
  width: 100%;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
}
.slides {
  display: flex;
  width: max-content;
}
.slide {
  scroll-snap-align: start;
  width: 100vw;
  height: 300px;
}
.slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>

使用 Vue 过渡动画

结合 Vue 的 <transition> 组件实现滑动动画:

<template>
  <div class="carousel">
    <transition :name="transitionName">
      <img :key="currentImage" :src="images[currentImage].src" :alt="images[currentImage].alt">
    </transition>
    <button @click="prev">Prev</button>
    <button @click="next">Next</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: [
        { src: 'image1.jpg', alt: 'Image 1' },
        { src: 'image2.jpg', alt: 'Image 2' },
        { src: 'image3.jpg', alt: 'Image 3' }
      ],
      currentImage: 0,
      transitionName: 'slide-next'
    };
  },
  methods: {
    next() {
      this.transitionName = 'slide-next';
      this.currentImage = (this.currentImage + 1) % this.images.length;
    },
    prev() {
      this.transitionName = 'slide-prev';
      this.currentImage = (this.currentImage - 1 + this.images.length) % this.images.length;
    }
  }
};
</script>

<style>
.carousel {
  position: relative;
  width: 100%;
  height: 300px;
  overflow: hidden;
}
.carousel img {
  position: absolute;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.slide-next-enter-active, .slide-next-leave-active,
.slide-prev-enter-active, .slide-prev-leave-active {
  transition: transform 0.5s ease;
}
.slide-next-enter-from {
  transform: translateX(100%);
}
.slide-next-leave-to {
  transform: translateX(-100%);
}
.slide-prev-enter-from {
  transform: translateX(-100%);
}
.slide-prev-leave-to {
  transform: translateX(100%);
}
</style>

以上方法可以根据项目需求选择适合的实现方式,Swiper 提供了最丰富的功能,纯 CSS 实现简单轻量,Vue 过渡动画则更适合需要自定义动画效果的场景。

标签: 图片vue
分享给朋友:

相关文章

vue优秀实现

vue优秀实现

Vue.js 优秀实现方法 响应式数据管理 使用 Vue 的 ref 和 reactive 处理基础类型和对象类型数据,结合 computed 计算属性优化渲染性能。对于复杂状态,推荐 Pinia 替…

vue实现handsontable

vue实现handsontable

Vue 中实现 Handsontable 要在 Vue 项目中集成 Handsontable,可以使用官方提供的 @handsontable/vue 封装库。以下是一个完整的实现步骤。 安装依赖 确…

vue 实现树状

vue 实现树状

Vue 实现树状结构的方法 递归组件实现树状结构 递归组件是 Vue 中实现树状结构的常见方法。通过组件自身调用自身,可以轻松构建多层级树状结构。 <template> <ul…

vue实现定时

vue实现定时

Vue 实现定时功能的方法 使用 setInterval 和 clearInterval 在 Vue 中可以通过 setInterval 和 clearInterval 实现定时功能。在组件的 mou…

vue 实现脚本

vue 实现脚本

Vue 实现脚本的方法 Vue.js 提供了多种方式来实现脚本功能,包括组件内脚本、混入(Mixins)、插件(Plugins)以及自定义指令等。以下是常见的实现方式: 组件内脚本 在 Vue 单文…

vue实现addclass

vue实现addclass

Vue 实现动态添加 class 的方法 在 Vue 中动态添加 class 可以通过多种方式实现,以下是常见的几种方法: 使用对象语法 通过绑定一个对象到 :class,可以动态切换 class…