当前位置:首页 > 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 Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的基本实现步骤和核心功能。 安装 Vue Router 通过…

vue 实现单选

vue 实现单选

使用 v-model 绑定单选值 在 Vue 中,可以通过 v-model 绑定单选按钮的值。定义一个响应式数据(如 selectedOption),并将其与单选按钮的 value 属性关联。…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…

vue实现筛选

vue实现筛选

实现筛选功能的基本思路 在Vue中实现筛选功能通常需要结合数据绑定、计算属性和方法。筛选的核心逻辑是根据用户输入的条件过滤原始数据列表,并动态更新显示结果。 数据准备 定义一个数组存储原始数据,另一…

vue实现xterm

vue实现xterm

在 Vue 中集成 Xterm.js Xterm.js 是一个基于 TypeScript 的前端终端组件库,可用于在浏览器中实现终端功能。以下是在 Vue 项目中集成 Xterm.js 的详细步骤。…

vue实现选人

vue实现选人

实现选人功能的基本思路 在Vue中实现选人功能通常涉及以下核心环节:数据绑定、用户交互处理、状态管理以及界面渲染。以下是具体实现方法: 数据准备与组件结构 创建包含人员信息的数组,通常从API获取或…