当前位置:首页 > VUE

vue实现图片预览效果

2026-02-24 15:15:45VUE

使用 v-viewer 插件实现图片预览

安装 v-viewer 插件:

npm install v-viewer

在 Vue 项目中全局引入:

vue实现图片预览效果

import VueViewer from 'v-viewer'
import 'viewerjs/dist/viewer.css'
Vue.use(VueViewer)

在组件中使用:

<template>
  <div>
    <img v-for="src in images" :src="src" :key="src" v-viewer>
  </div>
</template>

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

自定义图片预览组件

创建可点击放大的图片组件:

vue实现图片预览效果

<template>
  <div class="image-preview">
    <img 
      v-for="(img, index) in imgs" 
      :key="index" 
      :src="img" 
      @click="showPreview(index)"
      class="preview-image"
    >
    <div v-if="show" class="preview-modal" @click.self="closePreview">
      <img :src="currentImg" class="modal-content">
      <span class="close" @click="closePreview">&times;</span>
    </div>
  </div>
</template>

<script>
export default {
  props: ['imgs'],
  data() {
    return {
      show: false,
      currentIndex: 0
    }
  },
  computed: {
    currentImg() {
      return this.imgs[this.currentIndex]
    }
  },
  methods: {
    showPreview(index) {
      this.currentIndex = index
      this.show = true
    },
    closePreview() {
      this.show = false
    }
  }
}
</script>

<style scoped>
.preview-image {
  cursor: pointer;
  margin: 5px;
  max-height: 100px;
}
.preview-modal {
  position: fixed;
  z-index: 9999;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0,0,0,0.9);
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-content {
  max-width: 90%;
  max-height: 90%;
}
.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  cursor: pointer;
}
</style>

使用 Element UI 的图片预览

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

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

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

实现手势缩放功能

对于需要手势缩放的高级预览功能,可以结合 Hammer.js:

import Hammer from 'hammerjs'

mounted() {
  const image = this.$refs.previewImage
  const hammer = new Hammer(image)

  hammer.get('pinch').set({ enable: true })
  hammer.on('pinch', (e) => {
    // 实现缩放逻辑
    image.style.transform = `scale(${e.scale})`
  })
}

以上方法提供了从简单到复杂的多种图片预览实现方案,可根据项目需求选择合适的方案。

标签: 效果图片
分享给朋友:

相关文章

vue实现对话框效果

vue实现对话框效果

使用 Vue 实现对话框效果 在 Vue 中实现对话框效果可以通过组件化方式完成,通常结合 v-if/v-show 控制显示隐藏,以及插槽(slot)实现内容自定义。 基础对话框组件实现 创建一个名…

vue实现拖拉效果

vue实现拖拉效果

实现拖拽效果的基本方法 在Vue中实现拖拽效果可以通过原生HTML5的拖拽API或第三方库如vuedraggable来完成。以下是两种常见方法的详细说明。 使用HTML5拖拽API HTML5提供…

vue实现拖动图片验证

vue实现拖动图片验证

Vue 实现拖动图片验证功能 核心思路 拖动图片验证通常需要用户将图片拖动到指定区域或按正确顺序排列。实现时需要处理拖拽事件、验证逻辑和视觉反馈。 实现步骤 1. 基础组件结构 <tem…

vue实现放大图片

vue实现放大图片

实现图片放大功能的方法 在Vue中实现图片放大功能,可以通过多种方式实现。以下是几种常见的方法: 使用CSS transform属性 通过CSS的transform: scale()属性实现图…

vue效果实现

vue效果实现

Vue 效果实现方法 Vue.js 提供了多种方式实现动态效果,包括过渡动画、状态驱动的 UI 变化以及第三方动画库集成。以下是常见效果的实现方案: 过渡动画 使用 Vue 内置的 <tran…

h5页面滑动效果实现

h5页面滑动效果实现

实现H5页面滑动效果的方法 H5页面滑动效果可以通过多种方式实现,包括原生JavaScript、CSS动画或第三方库。以下是几种常见的方法: 使用CSS Scroll Snap CSS Scrol…