当前位置:首页 > VUE

vue 实现预览图片

2026-03-29 13:58:03VUE

实现图片预览功能

使用 v-viewer 插件

安装 v-viewer 插件:

npm install v-viewer

main.js 中引入并配置:

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

组件中使用:

vue 实现预览图片

<template>
  <div>
    <img v-for="src in images" :src="src" :key="src" @click="show">
    <viewer :images="images" ref="viewer">
      <template slot-scope="scope">
        <img v-for="src in scope.images" :src="src" :key="src">
      </template>
    </viewer>
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: ['image1.jpg', 'image2.jpg']
    }
  },
  methods: {
    show() {
      this.$refs.viewer.show()
    }
  }
}
</script>

使用原生 JavaScript 实现

创建图片预览组件:

<template>
  <div>
    <img 
      v-for="(img, index) in imgList" 
      :src="img" 
      :key="index"
      @click="previewImg(index)"
      class="preview-img"
    >
    <div v-if="showPreview" class="preview-container">
      <span class="close-btn" @click="closePreview">×</span>
      <img :src="currentImg" class="preview-content">
    </div>
  </div>
</template>

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

<style>
.preview-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.8);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}
.preview-content {
  max-width: 90%;
  max-height: 90%;
}
.close-btn {
  position: absolute;
  top: 20px;
  right: 20px;
  color: white;
  font-size: 30px;
  cursor: pointer;
}
.preview-img {
  cursor: pointer;
  margin: 5px;
  max-width: 100px;
  max-height: 100px;
}
</style>

使用第三方库 vue-photo-preview

安装:

vue 实现预览图片

npm install vue-photo-preview

使用方式:

import preview from 'vue-photo-preview'
import 'vue-photo-preview/dist/skin.css'
Vue.use(preview)

模板中使用:

<img v-for="src in images" :src="src" :key="src" v-preview="src">

实现图片缩放和旋转功能

扩展原生实现:

<template>
  <div class="preview-container" v-if="showPreview">
    <div class="toolbar">
      <button @click="zoomIn">放大</button>
      <button @click="zoomOut">缩小</button>
      <button @click="rotateLeft">左旋转</button>
      <button @click="rotateRight">右旋转</button>
      <button @click="closePreview">关闭</button>
    </div>
    <img 
      :src="currentImg" 
      class="preview-content"
      :style="{
        transform: `scale(${scale}) rotate(${rotate}deg)`,
        transition: 'all 0.3s'
      }"
    >
  </div>
</template>

<script>
export default {
  data() {
    return {
      scale: 1,
      rotate: 0
    }
  },
  methods: {
    zoomIn() {
      this.scale += 0.1
    },
    zoomOut() {
      if (this.scale > 0.2) {
        this.scale -= 0.1
      }
    },
    rotateLeft() {
      this.rotate -= 90
    },
    rotateRight() {
      this.rotate += 90
    },
    resetTransform() {
      this.scale = 1
      this.rotate = 0
    }
  }
}
</script>

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

相关文章

代码实现vue

代码实现vue

创建Vue项目 使用Vue CLI快速初始化项目,确保已安装Node.js和npm/yarn: npm install -g @vue/cli vue create my-vue-project c…

vue 实现全屏

vue 实现全屏

Vue 实现全屏功能 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 完成。以下是一个完整的实现方案: 安装依赖(可选) 如果需要跨浏览器兼容性更好,可以安装 scre…

vue实现keepalive

vue实现keepalive

Vue 中实现 keep-alive 在 Vue 中,keep-alive 是一个内置组件,用于缓存动态组件或组件的状态,避免重复渲染和销毁。以下是实现 keep-alive 的几种方法: 基本用法…

vue实现标注

vue实现标注

Vue 实现标注功能的方法 使用 Vue 实现标注功能可以通过多种方式实现,以下介绍几种常见的方法: 1. 使用 HTML5 Canvas 实现标注 Canvas 提供了强大的绘图能力,适合实现复…

vue实现评价

vue实现评价

Vue实现评价功能 数据绑定与双向绑定 Vue的核心特性是数据绑定和双向绑定,通过v-model指令可以轻松实现表单输入与应用状态之间的双向绑定。在评价功能中,可以使用v-model绑定评论文本和评分…

vue实现fragment

vue实现fragment

Vue 实现 Fragment 的方法 在 Vue 中,Fragment 允许组件返回多个根节点而不需要包裹一个额外的 DOM 元素。以下是几种实现方式: 使用 Vue 3 的 <templa…