当前位置:首页 > 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 实现标题的方法 在Vue中实现标题可以通过多种方式,以下是几种常见的方法: 方法一:使用模板语法 在Vue组件的模板中直接使用HTML的<h1>到<h6>标签来定义…

vue 实现遮罩

vue 实现遮罩

Vue 实现遮罩层的方法 使用固定定位和透明背景 在Vue中实现遮罩层可以通过CSS固定定位结合透明背景色完成。创建一个全屏遮罩组件,利用position: fixed覆盖整个视窗。 <tem…

vue实现github

vue实现github

Vue 实现 GitHub 相关功能 创建 GitHub API 请求 在 Vue 项目中,可以使用 axios 或 fetch 与 GitHub API 进行交互。需要在 GitHub 上创建个人访…

vue实现spa

vue实现spa

Vue 实现 SPA(单页应用)的步骤 创建 Vue 项目 使用 Vue CLI 快速初始化一个 Vue 项目,确保安装了 Vue Router 作为路由管理工具。运行以下命令创建项目: vue c…

前端vue实现概率

前端vue实现概率

Vue实现概率功能的方法 在Vue中实现概率功能通常涉及随机数生成和概率计算。以下是几种常见实现方式: 基础概率实现 使用Math.random()生成0到1之间的随机数,与设定概率比较: con…

vue歌词滚动实现

vue歌词滚动实现

实现 Vue 歌词滚动的核心方法 监听当前播放时间 通过 audio 元素的 timeupdate 事件获取当前播放时间,并与歌词时间戳对比。在 Vue 中使用 @timeupdate 绑定事件:…