当前位置:首页 > VUE

vue实现多个图片预览

2026-01-23 06:11:15VUE

Vue 实现多图片预览功能

使用第三方库(推荐)

安装 viewerjsvue-photo-preview 这类专门处理图片预览的库。

npm install viewerjs

在组件中引入并使用:

import Viewer from 'viewerjs'
import 'viewerjs/dist/viewer.css'

export default {
  mounted() {
    new Viewer(this.$refs.gallery)
  }
}

模板部分:

<div ref="gallery">
  <img v-for="(img, index) in images" :key="index" :src="img" width="100">
</div>

自定义模态框实现

创建一个图片预览组件:

vue实现多个图片预览

<template>
  <div class="preview-container" v-if="show">
    <div class="preview-overlay" @click="close"></div>
    <div class="preview-content">
      <img :src="currentImage" @click.stop>
      <button @click="prev">上一张</button>
      <button @click="next">下一张</button>
      <button @click="close">关闭</button>
    </div>
  </div>
</template>

<script>
export default {
  props: ['images'],
  data() {
    return {
      show: false,
      currentIndex: 0
    }
  },
  computed: {
    currentImage() {
      return this.images[this.currentIndex]
    }
  },
  methods: {
    open(index) {
      this.currentIndex = index
      this.show = true
    },
    close() {
      this.show = false
    },
    prev() {
      this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length
    },
    next() {
      this.currentIndex = (this.currentIndex + 1) % this.images.length
    }
  }
}
</script>

使用 Element UI 组件

如果项目使用 Element UI,可以直接使用其图片预览组件:

<template>
  <el-image 
    v-for="(img, index) in images"
    :key="index"
    :src="img"
    :preview-src-list="images"
  ></el-image>
</template>

<script>
export default {
  data() {
    return {
      images: [
        'image1.jpg',
        'image2.jpg',
        'image3.jpg'
      ]
    }
  }
}
</script>

移动端优化方案

对于移动端,建议添加手势支持:

methods: {
  handleTouchStart(e) {
    this.startX = e.touches[0].clientX
  },
  handleTouchEnd(e) {
    const endX = e.changedTouches[0].clientX
    if (this.startX - endX > 50) {
      this.next()
    } else if (endX - this.startX > 50) {
      this.prev()
    }
  }
}

模板添加触摸事件:

vue实现多个图片预览

<img 
  :src="currentImage" 
  @touchstart="handleTouchStart"
  @touchend="handleTouchEnd"
>

性能优化建议

对于大量图片,使用懒加载技术:

<img 
  v-for="(img, index) in images"
  :key="index"
  v-lazy="img"
  @click="open(index)"
>

安装 vue-lazyload

npm install vue-lazyload

在 main.js 中配置:

import VueLazyload from 'vue-lazyload'
Vue.use(VueLazyload, {
  preLoad: 1.3,
  loading: 'loading.gif',
  attempt: 1
})

标签: 多个图片
分享给朋友:

相关文章

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template>…

vue实现图片拖拽

vue实现图片拖拽

实现图片拖拽功能 在Vue中实现图片拖拽功能可以通过HTML5的拖放API结合Vue的事件处理来完成。以下是一个完整的实现方法: 基础拖拽实现 为图片元素添加draggable属性并绑定相关事件:…

css动画制作图片

css动画制作图片

CSS动画制作图片的方法 使用CSS动画可以为图片添加各种动态效果,提升网页的视觉吸引力。以下是几种常见的CSS动画实现方式: 关键帧动画(@keyframes) 通过定义关键帧来控制动画的各个阶…

网页设计制作css图片

网页设计制作css图片

CSS图片处理基础方法 在网页设计中,CSS可以控制图片的尺寸、位置、边框等样式属性。通过<img>标签或背景图方式引入图片后,使用CSS属性进行调整。 img { width:…

vue 实现图片

vue 实现图片

Vue 实现图片加载与展示的方法 在 Vue 中实现图片加载和展示可以通过多种方式完成,包括静态资源引用、动态绑定、懒加载等。以下是几种常见的方法: 静态资源引用 将图片放在项目的 public…

jquery图片

jquery图片

jQuery 图片操作 jQuery 提供了多种方法来处理图片,包括加载、显示、隐藏、动画效果等。以下是一些常见的 jQuery 图片操作方法: 动态加载图片 使用 jQuery 的 attr()…